Re: sys.excepthook and threads

Discussion in 'Python' started by Jonathan Ellis, Oct 5, 2004.

  1. I'm running into the same problem Chuck did 1.5 years ago. Can anyone
    shed any light on why threads ignore sys.excepthook? Is there a better
    workaround than the following? (prefixing whitespace with . to prevent
    google from munching it)

    import sys
    from threading import Thread

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

    run_old = getattr(Thread, 'run')
    def run(self, *args, **kargs):
    .. try:
    .. run_old(self, *args, **kargs)
    .. except:
    .. MyHandler(*sys.exc_info())
    setattr(Thread, 'run', run)

    -Jonathan

    Chuck Esterbrook wrote:
    > 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
    Jonathan Ellis, Oct 5, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Follower
    Replies:
    1
    Views:
    3,027
    Jeremy Fincher
    Oct 27, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    563
    Tim Peters
    Oct 30, 2003
  3. Jesus Rivero - (Neurogeek)

    Threads and sys.excepthook

    Jesus Rivero - (Neurogeek), Mar 28, 2006, in forum: Python
    Replies:
    1
    Views:
    425
    Jonathan Ellis
    Mar 28, 2006
  4. ian

    sys.excepthook and threads

    ian, Mar 27, 2007, in forum: Python
    Replies:
    1
    Views:
    322
    Dr. Peer Griebel
    Apr 12, 2007
  5. Jai
    Replies:
    1
    Views:
    150
    Neil Cerutti
    Nov 19, 2013
Loading...

Share This Page