Handling a thread’s exception in the caller thread in Python
Last Updated :
22 Nov, 2021
Multithreading in Python can be achieved by using the threading library. For invoking a thread, the caller thread creates a thread object and calls the start method on it. Once the join method is called, that initiates its execution and executes the run method of the class object.
For Exception handling, try-except blocks are used that catch the exceptions raised across the try block and are handled accordingly in the except block
Example:
Python3
import threading
import sys
class MyThread(threading.Thread):
def someFunction( self ):
print ( "Hello World" )
def run( self ):
self .someFunction()
def join( self ):
threading.Thread.join( self )
def main():
t = MyThread()
t.start()
t.join()
if __name__ = = '__main__' :
main()
|
Output:
Hello World
For catching and handling a thread’s exception in the caller thread we use a variable that stores the raised exception (if any) in the called thread, and when the called thread is joined, the join function checks whether the value of exc is None, if it is then no exception is generated, otherwise, the generated exception that is stored in exc is raised again. This happens in the caller thread and hence can be handled in the caller thread itself.
Example: The example creates a thread t of type MyThread, the run() method for the thread calls the someFunction() method, that raises the MyException, thus whenever the thread is run, it will raise an exception. To catch the exception in the caller thread we maintain a separate variable exc, which is set to the exception raised when the called thread raises an exception. This exc is finally checked in the join() method and if is not None, then join simply raises the same exception. Thus, the caught exception is raised in the caller thread, as join returns in the caller thread (Here The Main Thread) and is thus handled correspondingly.
Python3
import threading
import sys
class MyException(Exception):
pass
class MyThread(threading.Thread):
def someFunction( self ):
name = threading.current_thread().name
raise MyException( "An error in thread " + name)
def run( self ):
self .exc = None
try :
self .someFunction()
except BaseException as e:
self .exc = e
def join( self ):
threading.Thread.join( self )
if self .exc:
raise self .exc
def main():
t = MyThread()
t.start()
try :
t.join()
except Exception as e:
print ( "Exception Handled in Main, Details of the Exception:" , e)
if __name__ = = '__main__' :
main()
|
Output:
Exception Handled in Main, Details of the Exception: An error in thread Thread-1
Please Login to comment...