I am trying to handle uncaught exceptions that occur when I run a thread. The python documentation at docs.python.org states that "threading.excepthook() can be overridden to control how uncaught exceptions raised by Thread.run() are handled." However, I can't seem to do it properly. It doesn't appear that my excepthook function is ever excecuted. What is the correct way to do this?
import threading
import time
class MyThread(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def excepthook(self, *args, **kwargs):
print("In excepthook")
def error_soon(timeout):
time.sleep(timeout)
raise Exception("Time is up!")
my_thread = MyThread(target=error_soon, args=(3,))
my_thread.start()
time.sleep(7)