Exceptions in threads

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Jan 24 09:47:50 EST 2002


Dale Strickland-Clark <dale at riverhall.NOTHANKS.co.uk> writes:

> What happens when a thread throws an exception?

The stack in the thread will be unwound. If the exception falls off
the end of the stack, a message will be printed to sys.stderr.

> Is it possible for the creating thread to catch it - or even be
> aware of it?

If the exception is handled within the thread also, why should it?

If the exception is not handled, the creating thread will not be aware
of it.

> Alternatively, is it possible for a thread to establish a catch-all
> exception handler? I realise that I can effectively do this in code
> but I always get this feeling that it wouldn't be so robust. :-/

How would you establish an exception handler by other means but code?

Just use

def thread_wrapper(*args):
  try:
    original_function(*args)
  except:
    global error_occurred
    error_occurred = sys.exc_info()

as your thread function.

Regards,
Martin



More information about the Python-list mailing list