Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to catch a thread's exception in the caller thread in Python?

I have the following script which copies files from one location to another.

If the files cannot be copied, an exception is thrown.

The following code does not work:

class TheThread(threading.Thread):
    def __init__(self, FromFolder, ToFolder):
        threading.Thread.__init__(self)
        self.FromFolder = FromFolder
        self.ToFolder = ToFolder
    def run(self):
        try:
           shul.copytree(self.FromFolder, self.ToFolder)
        except:
           raise
try:
    threadClass = TheThread(arg1, arg2,…)
    threadClass.start()   #Exception occurs here
except:
    print "An exception is caught"

How to catch the exception?


1 Answer
Manogna

The problem is that thread_obj.start() returns immediately. The child thread that you started executes in its own context, in its own stack. Any exception that occurs there is in the context of the child thread. You have to communicate this information to the parent thread by passing some message.

The code can be re-written as follows:

import sys
import threading
import Queue
class ExcThread(threading.Thread):
def __init__(self, foo):
threading.Thread.__init__(self)
self.foo = foo
def run(self):
try:
raise Exception('An error occurred here.')
except Exception:
self.foo.put(sys.exc_info())
def main():
foo = Queue.Queue()
thread_obj = ExcThread(foo)
thread_obj.start()


while True:
try:
exc = foo.get(block=False)
except Queue.Empty:
pass
else:
exc_type, exc_obj, exc_trace = exc
print exc_type, exc_obj
print exc_trace


thread_obj.join(0.1)
if thread_obj.isAlive():
continue



Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.