[python-win32] sys.excepthook not working as expected
Larry Bates
lbates@syscononline.com
Fri, 21 Feb 2003 11:37:42 -0600
I have tried unsuccessfully to set my own function to handle uncaught
exceptions.
Below is a short test program that doesn't work as expected:
import sys
def Myexcepthook(type, value, traceback):
print "in Myexcepthook"
import traceback
lines=traceback.format_exception(type, value, traceback)
print "---------------------Traceback lines-----------------------"
print "\n".join(lines)
print "-----------------------------------------------------------"
sys.exit(2)
#
# set sys.excepthook
#
sys.excepthook=Myexcepthook
#
# create an uncaught divide by zero exception
#
a=1/0
#---------------------End of program------------------------
When I execute this program I get:
in Myexcepthook
Error in sys.excepthook:
Traceback (most recent call last):
File "excepthooktest.py", line 6, in Myexcepthook
lines=traceback.format_exception(type, value, traceback)
File "C:\Python22\Lib\traceback.py", line 140, in format_exception
list = list + format_tb(tb, limit)
File "C:\Python22\Lib\traceback.py", line 75, in format_tb
return format_list(extract_tb(tb, limit))
File "C:\Python22\Lib\traceback.py", line 94, in extract_tb
f = tb.tb_frame
AttributeError: 'module' object has no attribute 'tb_frame'
Original exception was:
Traceback (most recent call last):
File "excepthooktest.py", line 20, in ?
a=1/0
ZeroDivisionError: integer division or modulo by zero
If I change:
lines=traceback.format_exception(type, value, traceback)
to
lines=traceback.format_exception_only(type, value)
Myexcepthook function behaves properly and prints:
---------------------Traceback lines-----------------------
ZeroDivisionError: integer division or modulo by zero
-----------------------------------------------------------
Anyone have any ideas. I would like to capture the traceback information as
well as the exception.
Also, the sys.excepthook override doesn't seem to work at all when I run
this interactively, only works if run from console.
BTW - I'm using ActiveState PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39)
Thanks in advance,
Larry Bates
lbates@syscononline.com