By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

capture exception raised by child thread.

P: n/a
Hi,

I have a class that created a child thread for processing, this thread might raise an exception under some condition. On the main program that create an object of this case, I put a try/except block trying to capture the exception but failed. Am I doing anything wrong here?

here is the stripped version of my code:

class A:
def __init__(self):
self.thread = threading.Thread(None, self.MyThread)
self.thread.setDaemon(1)
self.thread.start()

def MyThread(self):
while 1:
# do something. if error:

if error:
raise exception

in my Main program:

main()
a = A()
while 1:
try:
# do something..
except exception, e:
# I should capture the exception from A, but not..
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Joe Wong wrote:
Hi,

I have a class that created a child thread for processing, this thread might raise an exception under some condition. On the main program that
create an object of this case, I put a try/except block trying to capture
the exception but failed. Am I doing anything wrong here?

See the modifications to your code. I hope I got all the
names right, it's been a while, but it should give you an idea
about one way how to do it.

here is the stripped version of my code:

class A:
def __init__(self):
self.thread = threading.Thread(None, self.MyThread)
self.thread.setDaemon(1)
It's a bit unusual for a deamon thread to end in an exception,
but that doesn't matter.

self.caughtException = None
self.endEvent = Event() # from the threading module iirc. self.thread.start()

def MyThread(self): try:
try: while 1:
# do something. if error:

if error:
raise exception except exc:
self.caughtException = exc
# evt. raise exc again
finally:
self.endEvent.set()

in my Main program:

main()
a = A()
while 1: # do something..


# test (or wait) for the event from a here.
if a.endEvent.isSet() and a.caughtException:
...

Have fun,
Ype

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.