try except question - serious foo bar question, and pulling my hair out :-)

Laszlo Nagy gandalf at shopzeus.com
Tue Feb 24 10:45:13 EST 2009


>>
>> Use this instead:
>>
>> import sys
>> try:
>>     ???
>> except:
>>    e = sys.exc_value
>>    do_with(e)
>
> Only at the outermost block on your code, if ever... The fact that 
> some exceptions don't inherit from Exception is on purpose -- usually 
> you *dont* want to catch (and swallow) SystemExit (nor 
> KeyboardInterrupt, and a few more I think)
>

Yes, this was at the outermost block of my thread's run method.

SystemExit exception did not generate an "exception in Thread-32" type 
traceback on stderr because for Python, it seemed to be a "normal" exit 
of the thread. So I wrote an except block, to see why the thread has 
been stopping. In this case, catching SystemExit was desired. The 
problem was in my mind: I thought that "except Exception,e:" will catch 
everything.

The solution was to replace "raise SystemExit(0)" with "raise 
TransportClosedException()" in another class.

Thanks again,

   Laszlo




More information about the Python-list mailing list