Last active
September 6, 2021 08:04
-
-
Save etherealite/027826efaaaf0f46b42edc7eafb5f27a to your computer and use it in GitHub Desktop.
This file contains 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 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