Python 2.5b2 and excepthook problem

Larry Bates larry.bates at websafe.com
Mon Aug 7 12:56:16 EDT 2006


I have some classes that trap hook sys.excepthook and log
exceptions prior to the program exiting.  This has proven
to be an effective way to log what is going on with many
of my "lights out" processes.  I'm doing some testing with
Python 2.5b2 and can't seem to get it to work properly.
Here is a very small script that I think should work but
doesn't.

import sys

def excepthook(type, value, tb):
    import traceback
    print "entering my excepthook function"
    tblines=traceback.format_exception(type, value, tb)
    #traceback.print_tb(tb)
    return

if __name__=="__main__":
    sys.excepthook=excepthook
    print "sys.excepthook=", sys.excepthook
    a=[1,2,3]
    b=a[4]

When I run this I get:

sys.excepthook= <function excepthook at 0x0140E070>
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "K:\SYSCON\PYTHON\ZBKUP\junk.py", line 14, in <module>
    b=a[4]
IndexError: list index out of range
>>>

The sys.excepthook is clearly pointing to my function, but when
the exception occurs the function isn't called because the print
statement isn't executed upon entering the function.  Has something
changed that I missed?

Thanks in advance for any assistance.

Regards,
Larry Bates



More information about the Python-list mailing list