I already replaced sys.excepthook
by a custom exception handler, which works fine :
import logging;
import sys;
logger = logging.getLogger('my_logger');
# Creating handler
handler_print = logging.StreamHandler();
handler_print.setLevel(logging.DEBUG);
logger.addHandler(handler_print);
# creating custom error handler function
def custom_err_handler(exc_type, exc_value, exc_traceback):
logger.critical("Custom error handler :", exc_info=(exc_type, exc_value, exc_traceback));
sys.excepthook = custom_err_handler;
integer = 3;
print("Value is" + integer); # Custom exception handler called (TypeError: can only concatenate str (not "int") to str)
My problem is to use this custom handler within a thread :
import threading;
threading.excepthook = custom_err_handler;
class Test_Class(threading.Thread):
def run(self):
integer = 3;
print("Value is" + integer); # Custom exception handler called
test_class = Test_Class();
test_class.start();
Returns :
Custom error handler : Traceback (most recent call last): File "C:\Program Files\Python3.9\lib\threading.py", line 1214, in invoke_excepthook hook(args) TypeError: custom_err_handler() missing 2 required positional arguments: 'exc_value' and 'exc_traceback'
sys.excepthook
and threading.excepthook
does not seem to expect the same parameters.
How can I use un custom handler for exception raised both inside and outside threads ?