How to get a raised exception from other thread

Peter Hansen peter at engcorp.com
Mon Oct 17 20:02:48 EDT 2005


dcrespo wrote:
>>Define what "get" means for your purposes.  It appears that you mean you
>>want to catch the exception, but in the thread which launched the other
>>thread in the first place.  If that's true, please show where you would
>>expect to catch this exception, given that when you start the thread,
>>the main thread continues running and might even finish before the other
>>thread finishes.
> 
> In my above example, I want to catch in ProgramA the raised exception
> in ProgramB. 

Better not to call them "programs" since they're not separate programs, 
just separate modules, one of which starts a thread using methods in the 
other module.

And you haven't really answered the questions I asked.  Please show 
exactly where you expect to catch the exception, given the points I note 
above.  Before, after, or during the .start() call, or somewhere else?

> Please, look at my example one more time.

Done.  It didn't really help looking at it a third time.  It's still 
unclear what you want.

Maybe explaining the use case would help you help us help you. :-)

Your example seems contrived.  In the real code you want to write, why 
do you expect an exception to occur?  Will it be raised deliberately, as 
you show in the example, or is it unexpected (and unhandled by any 
try/except at the top level of the thread's run() method)?  Or do you 
really need to use an exception here?

I'm quite sure the problem you are trying to solve can be solved, but 
you are still describing part of the solution you believe you need, 
rather than explaining why you want to do this (which may let us show 
you other, simpler or cleaner ways to accomplish your goals).

-Peter



More information about the Python-list mailing list