Created
January 6, 2020 03:19
-
-
Save linw1995/d85801c5db792a77ae2bc1f2ee8a538b to your computer and use it in GitHub Desktop.
Use `threading.trace` to catch uncatched exception
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import threading | |
| def raise_from_top(): | |
| raise RuntimeError | |
| def raise_from_second_frame(): | |
| raise_from_top() | |
| def raise_from_third_frame(): | |
| raise_from_second_frame() | |
| def trace(frame, event, arg): | |
| if event == "exception": | |
| print(frame, event, arg) | |
| return trace | |
| threading.settrace(trace) | |
| for target in [raise_from_top, raise_from_second_frame, raise_from_third_frame]: | |
| threading.Thread(target=target).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment