< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039199167010047123209178152124239215162147037129069022086011090

Standard Exceptions (Getting Help from Other Modules)

Apart from the exception module, other Python modules offer you some advanced functionality to handle exceptions. We will talk about the sys and the traceback modules.

You can use the sys.exc_info() thread-safe function to get information about the current exception being handled. This function returns a tuple of values that is equivalent to the values provided by three other sys module objects:

sys.exc_type—Returns the exception type

sys.exc_value—Returns the exception value

sys.exc_traceback—Returns a traceback object

					
Note that these objects only work when called from within an except clause.>>>
import sys
>>> try:
…       1/0
… except:
…     print sys.exc_type, ":", sys.exc_value
exceptions.ZeroDivisionError : integer division or modulo

				

The last example can also be implemented as

					
>>> import sys
>>> try:
…       1/0
…   except:
…       info = sys.exc_info()
…       exc_type = info[0]
…       exc_value = info[1]
…       exc_traceback = info[2]
…       print exc_type, ":", exc_value
…
exceptions.ZeroDivisionError : integer division or modulo

				

A more compact way to assign the values to the variables is by using sequence unpacking, as is demonstrated by the following:

					
exc_type, exc_value, exc_traceback = self.exc_info()

				

The Python module called traceback, which is part of the standard Python library, helps you to debug the call stack after an exception has been raised.

					
 1: >>> import traceback
 2: >>> try:
 3: …       1/0
 4: …   except:
 5: …       print "The next lines show the traceback message"
 6: …       print "-----------------------------------------"
 7: …       traceback.print_exc()
 8: …       print "-----------------------------------------"
 9: …
10: The next lines show the traceback message
11: -----------------------------------------
12: Traceback (innermost last):
13:   File "<stdin>", line 2, in ?
14: ZeroDivisionError: integer division or modulo
15: -----------------------------------------

				

The previous program chooses the right time to display the traceback message by using the traceback.print_exc() function (line 7).

You can also extract the traceback information by parsing the results of sys.exc_traceback.

					
>>> import sys, traceback
>>> try:
…          result = 1/0
…   except:
…          trace = traceback.extract_tb(sys.exc_traceback)
…           for filename, lineno,function,message in trace:
…              print "File name: ", filename
…              print "Error message: ", message
…              print "Line: ", lineno
…              print "Function: ", function
…

				

By using the objects sys.last_type, sys.last_value, and sys.last_traceback, you can get the details about the last uncaught exception. When I say that, I mean the last exception that had a traceback message displayed.

					
>>> import sys
>>> x = 0
>>> 1 / x
Traceback (innermost last):
  File "<stdin>", line 1, in ?
ZeroDivisionError: integer division or modulo
>>> 1.0 / 10
0.1
>>> print sys.last_type
exceptions.ZeroDivisionError
>>> print sys.last_value
integer division or modulo

				


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

exceptions
      standard
      uncaught
functions
      traceback.print_exc()
modules
      sys 2nd 3rd
      tradeback 2nd
objects
     sys module
            values 2nd
      sys.last_traceback
      sys.last_type
      sys.last_value
standard exceptions
sys module 2nd 3rd
sys.exe_traceback value 2nd
sys.exe_type value
sys.exe_value value
sys.last_traceback object
sys.last_type object
sys.last_value object
traceback.print_exc() function
tradeback module 2nd
uncaught exceptions
values
      sys module objects 2nd
      sys.exe_traceback 2nd
      sys.exe_type
      sys.exe_value

© 2002, O'Reilly & Associates, Inc.