Skip to content

Instantly share code, notes, and snippets.

@linw1995
Created January 6, 2020 03:19
Show Gist options
  • Select an option

  • Save linw1995/d85801c5db792a77ae2bc1f2ee8a538b to your computer and use it in GitHub Desktop.

Select an option

Save linw1995/d85801c5db792a77ae2bc1f2ee8a538b to your computer and use it in GitHub Desktop.
Use `threading.trace` to catch uncatched exception
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