worker thread catching exceptions and putting them in queue

Stargaming stargaming at gmail.com
Mon Mar 5 12:00:04 EST 2007


Paul Sijben schrieb:
> All,
> 
> in a worker thread setup that communicates via queues is it possible to
> catch exceptions raised by the worker executed, put them in an object
> and send them over the queue to another thread where the exception is
> raised in that scope?
> 
> considering that an exception is an object I feel it ought to be
> possible, however I do not see how to go about it.
> 
> does anyone have a pointer towards the solution?
> 
> Paul

You're right, even exceptions are objects in Python. For further 
studies, read http://docs.python.org/lib/module-exceptions.html

You can catch an exception like this:
try:
   worker.do_some_work_that_may_raise_an_exception()
except Exception, e:
   # the first argument is the type of error you want to handle
   # it is Exception here, the baseclass of all computation exceptions
   # the second argument is the variable (name) where you want to save
   # the specific exception raised to
   # it's 'e' here, a common shortcut for exception
   exception_handler.handle(e)
   # notice that you can pass e around as you like

For further information on exceptions and how to handle them, read 
chapter 8 of the tutorial, especially starting from 8.3: 
http://docs.python.org/tut/node10.html#SECTION0010300000000000000000

P.S. I don't know if what I told still applies to Python 3.0 -- a lot of 
changes are upcoming related to exception raising and handling.



More information about the Python-list mailing list