10.12. Exceptions and the sys ModuleAn alternative way of obtaining exception information is by accessing the exc_info() function in the sys module. This function provides a 3-tuple of information, more than what we can achieve by simply using only the exception argument. Let us see what we get using sys.exc_info(): >>> try: What we get from sys.exc_info() in a tuple are:
The first two items we are familiar with: the actual exception class and this particular exception's instance (which is the same as the exception argument which we discussed in the previous section). The third item, a traceback object, is new. This object provides the execution context of where the exception occurred. It contains information such as the execution frame of the code that was running and the line number where the exception occurred. In older versions of Python, these three values were available in the sys module as sys.exc_type, sys.exc_value, and sys.exc_traceback. Unfortunately, these three are global variables and not thread-safe. We recommend using sys.exc_info() instead. All three will be phased out and eventually removed in a future version of Python. |
Wednesday, October 14, 2009
Section 10.12. Exceptions and the sys Module
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment