python thread exception

Solutions on MaxInterview for python thread exception by the best coders in the world

showing results for - "python thread exception"
Leonie
24 Jun 2018
1import sys
2import threading
3import Queue
4
5
6class ExcThread(threading.Thread):
7
8    def __init__(self, bucket):
9        threading.Thread.__init__(self)
10        self.bucket = bucket
11
12    def run(self):
13        try:
14            raise Exception('An error occured here.')
15        except Exception:
16            self.bucket.put(sys.exc_info())
17
18
19def main():
20    bucket = Queue.Queue()
21    thread_obj = ExcThread(bucket)
22    thread_obj.start()
23
24    while True:
25        try:
26            exc = bucket.get(block=False)
27        except Queue.Empty:
28            pass
29        else:
30            exc_type, exc_obj, exc_trace = exc
31            # deal with the exception
32            print exc_type, exc_obj
33            print exc_trace
34
35        thread_obj.join(0.1)
36        if thread_obj.isAlive():
37            continue
38        else:
39            break
40
41
42if __name__ == '__main__':
43    main()
44
Simon
27 Sep 2016
1try:
2    threadClass = TheThread(param1, param2, etc.)
3    threadClass.start()   ##### **Exception takes place here**
4except:
5    print "Caught an exception"
6
Humberto
12 Sep 2018
1class TheThread(threading.Thread):
2    def __init__(self, sourceFolder, destFolder):
3        threading.Thread.__init__(self)
4        self.sourceFolder = sourceFolder
5        self.destFolder = destFolder
6    
7    def run(self):
8        try:
9           shul.copytree(self.sourceFolder, self.destFolder)
10        except:
11           raise
12
similar questions
queries leading to this page
python thread exception