Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > capture exception raised by child thread.

Reply
Thread Tools

capture exception raised by child thread.

 
 
Joe Wong
Guest
Posts: n/a
 
      05-14-2004
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..
 
Reply With Quote
 
 
 
 
Ype Kingma
Guest
Posts: n/a
 
      05-14-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page not displaying when exception raised pbo.spam@gmail.com ASP .Net 5 11-07-2005 02:13 PM
: an exception raised =?Utf-8?B?SklNLkgu?= ASP .Net 9 06-03-2005 03:22 PM
exception raised for valid variable declaration? Carlos ASP .Net 1 02-17-2005 08:26 PM
Exception feature creep! (was: re-entering in the normal flow after an exception is raised) Lonnie Princehouse Python 8 10-02-2004 09:16 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments