capture exception raised by child thread.

Discussion in 'Python' started by Joe Wong, May 14, 2004.

  1. Joe Wong

    Joe Wong Guest

    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..
    Joe Wong, May 14, 2004
    #1
    1. Advertising

  2. Joe Wong

    Ype Kingma Guest

    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
    Ype Kingma, May 14, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,084
    David Schwartz
    Dec 9, 2003
  2. Lonnie Princehouse
    Replies:
    8
    Views:
    525
    Lonnie Princehouse
    Oct 2, 2004
  3. pegazik

    Exception raised in wrong thread?

    pegazik, Sep 19, 2005, in forum: Python
    Replies:
    1
    Views:
    258
    Piet van Oostrum
    Sep 20, 2005
  4. dcrespo
    Replies:
    28
    Views:
    845
    dcrespo
    Oct 22, 2005
  5. Ziliang Chen
    Replies:
    3
    Views:
    140
    Ziliang Chen
    Feb 22, 2013
Loading...

Share This Page