0

How can you catch an exception thrown by another thread in Python?

If I start another thread in "try" block and that thread calls an exception, "except" block doesn't catch it. How can I fix it? Sorry if it's a dumb question šŸ„“

29th Aug 2020, 9:52 PM
nedoyoba
nedoyoba - avatar
3 Answers
0
Do not apologize, and it is not a dumb question, multi-threading is a complicated topic. If you're using Python 3.8 you can try to use: "threading.excepthook(args, /)" It is used to "Handle uncaught exception raised by Thread.run()." https://docs.python.org/3/library/threading.html#threading.excepthook ps: you can also try this: https://www.tutorialspoint.com/How-to-catch-a-thread-s-exception-in-the-caller-thread-in-Python
29th Aug 2020, 10:04 PM
Steven M
Steven M - avatar
0
You can also use parent exception handling except for exception which is not covered by previous exception handlers.. Try : .... except (Exception 1) : .... except (exception n) : except : #every other exception can be caught here..
29th Aug 2020, 10:18 PM
Jayakrishna šŸ‡®šŸ‡³
0
Steven M tnx for answer, excepthook doesn't seem to handle an exception by 'except' block in a main thread, does it? I've tried it and it catches the exception but doesn't give the exception to 'except' block anyway, though that's all I need
31st Aug 2020, 2:17 PM
nedoyoba
nedoyoba - avatar