sys.excepthook and threads

Chuck Esterbrook ChuckEsterbrook at yahoo.com
Mon Apr 14 02:58:47 EDT 2003


Is there a reason why sys.excepthook does not get invoked by threads 
(other than the main thread)?

The program below does not output 'caught exception', but will if 
Raise() is called directly. Tested on Python 2.2.2 and 2.3a2. A quick 
search of c.l.p showed mention of this, but no explanation.  I'm just 
curious.

BTW the docs for sys.excepthook don't mention this phenomena at all. 
They say "When an exception is raised and uncaught, the interpreter 
calls sys.excepthook with three arguments" so that's what I expected, 
threads or not.

-----------------------------------------------
def MyHandler(typ, exc, tb):
    print 'caught exception'

import sys
sys.excepthook = MyHandler

from threading import Thread

def Raise():
    assert 0

if 1:
    Thread(target=Raise).start()
else:
    Raise()
-----------------------------------------------


-- 
Chuck
http://ChuckEsterbrook.com






More information about the Python-list mailing list