forcing exceptions

Gary Herron gherron at islandtraining.com
Fri Mar 3 14:44:49 EST 2006


Nikola Skoric wrote:

>Is there a way to tell the interpreter to display exceptions, even those 
>which were captured with except?
>
>  
>
Yes, sort of ...  You have to trigger the display yourself within the 
capturing "except" -- it's not automatic.

The traceback module provides a number of functions that can be used to 
display a traceback of the current (or any other) exception.   In 
addition, it can produce a traceback of the current state of the 
execution stack even without an exception:

Example (type this into the Python interpreter):

import traceback
try:
   assert False
except:
   traceback.print_exc()

and you'll get a traceback (a very short one in this case):
 
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
AssertionError

Gary Herron





More information about the Python-list mailing list