54
Python Module of the Week, Release 1.132
return a / divisor
ZeroDivisionError: integer division or modulo by zero
19.2.5 Exception Properties
In addition to the local variables from each stack frame,cgitb shows all properties of the exception object. If you
have a custom exception type with extra properties,they are printed as part ofthe error report.
import cgitb
cgitb.enable(format=’text’)
class MyException(Exception):
"""Add extra properties to a special exception
"""
def __init__(self, message, , bad_value):
self.bad_value = bad_value
Exception.__init__(self, message)
return
raise MyException(’Normal message’, bad_value=99)
In this example,the bad_value property is included along with the standard message and args values.
$ python cgitb_exception_properties.py
<class ’__main__.MyException’>
Python 2.7.2: /Users/dhellmann/Envs/pymotw/bin/python
Thu Feb 21 06:35:39 2013
A problem occurred in a Python script.
Here is the sequence of
function calls leading up to the error, in the order they occurred.
/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/cgitb/cgitb_exception_properties.py in <module>()
18
self.bad_value = bad_value
19
Exception.__init__(self, message)
20
return
21
22 raise MyException(’Normal message’, bad_value=99)
MyException = <class ’__main__.MyException’>
bad_value undefined
<class ’__main__.MyException’>: Normal message
__class__ = <class ’__main__.MyException’>
__delattr__ = <method-wrapper ’__delattr__’ of MyException object>
__dict__ = {’bad_value’: 99}
__doc__ = ’Add extra properties to a special exception\n
’
__format__ = <built-in method __format__ of MyException object>
__getattribute__ = <method-wrapper ’__getattribute__’ of MyException object>
__getitem__ = <method-wrapper ’__getitem__’ of MyException object>
__getslice__ = <method-wrapper ’__getslice__’ of MyException object>
__hash__ = <method-wrapper ’__hash__’ of MyException object>
__init__ = <bound method MyException.__init__ of MyException(’Normal message’,)>
__module__ = ’__main__’
__new__ = <built-in method __new__ of type object>
__reduce__ = <built-in method __reduce__ of MyException object>
__reduce_ex__ = <built-in method __reduce_ex__ of MyException object>
__repr__ = <method-wrapper ’__repr__’ of MyException object>
19.2. cgitb – Detailed traceback reports
559