Skip to content

Instantly share code, notes, and snippets.

@etherealite
Last active September 6, 2021 08:04
Show Gist options
  • Save etherealite/027826efaaaf0f46b42edc7eafb5f27a to your computer and use it in GitHub Desktop.
Save etherealite/027826efaaaf0f46b42edc7eafb5f27a to your computer and use it in GitHub Desktop.
import random, inspect
def func_a():
if bool(random.getrandbits(1)):
func_b()
else:
func_c()
def func_b():
raise Exception('Bang!')
def func_c():
raise Exception('Bang!')
try:
func_a()
except Exception as e:
frame = inspect.trace()[-1].frame
throwing_func = func_b if frame.f_code is func_b.__code__ else func_c
if throwing_func is func_b:
print('caught from func_b')
else:
print('caught from func_c')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment