[Tutor] Unable to catch exception

Lie Ryan lie.1296 at gmail.com
Wed Aug 13 11:56:19 CEST 2008


> Message: 7
> Date: Sun, 10 Aug 2008 18:16:38 -0400
> From: James <jtp at nc.rr.com>
> Subject: Re: [Tutor] Unable to catch exception
> To: "Kent Johnson" <kent37 at tds.net>
> Cc: tutor at python.org
> Message-ID:
>         <e107b4ff0808101516y59b52dafweb6d7a4eb9cdc0cc at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> Kent,
> 
> I'm not importing BadStatusLine. I'm only importing mechanize, which
> downloads a page repeatedly. From time to time a BadStatusLine
> exception is raised. I'm unsure how to go about catching it.
> 
> Thoughts?

Did you say you used threads to download the page (i.e. you used thread
or threading module? If so, then move the exception handler INSIDE the
thread or threading code because exception handler only catch exception
in its own thread.

so instead of this: (untested) 
#!/usr/bin/env python
import time
from threading import Thread

class MyThread(Thread):
    def __init__(self,bignum):
        Thread.__init__(self)
    
    def run(self):
        while True:
            if someCondition:
                raise AnError

if __name__=="__main__":
    try:
        MyThread().start()
    except AnError:
        print 'Got Error'

do this instead:
#!/usr/bin/env python
import time
from threading import Thread

class MyThread(Thread):
    def __init__(self,bignum):
        Thread.__init__(self)
    
    def run(self):
        try:
            while True:
                if someCondition:
                    raise AnError
        except AnError:
            print 'Got Error'
if __name__=="__main__":
    MyThread().start()

> 
> -j
> 
> On Sun, Aug 10, 2008 at 1:54 PM, Kent Johnson <kent37 at tds.net> wrote:
> > On Sat, Aug 9, 2008 at 12:28 PM, James <jtp at nc.rr.com> wrote:
> >> All,
> >>
> >> I'm having a rather strange problem that I'm hoping someone can
> shed
> >> some light on. I'm trying to catch a BadStatusLine exception raised
> by
> >> the httplib library.
> >> The error only happens once every blue moon, but to avoid a crash I
> >> setup a try/catch.
> >>
> >> try:
> >>   <download page code here>
> >> catch BadStatusLine:
> >>   print "yuck!"
> >>
> >> However, somehow the thread that this code is in still raised a
> >> BadStatusLine exception and the thread stopped cold.
> >
> > How did you import BadStatusLine?
> >
> > One strange gotcha about except statements is that the except clause
> > is not evaluated until an exception is raised, so you can have an
> > invalid name and you won't know it. For example this runs fine, even
> > though foo is not defined:
> > In [1]: try:
> >   ...:     1
> >   ...: except foo:
> >   ...:     pass
> >   ...:
> > Out[1]: 1
> >
> > OTOH if an exception is raised it causes a NameError:
> > In [2]: try:
> >   ...:     1/0
> >   ...: except foo:
> >   ...:     pass
> >   ...:
> >
> ---------------------------------------------------------------------------
> > <type 'exceptions.NameError'>             Traceback (most recent
> call last)
> >
> > /Users/kent/<ipython console> in <module>()
> >
> > <type 'exceptions.NameError'>: name 'foo' is not defined
> >
> > Kent
> >
> 



More information about the Tutor mailing list