我有一个典型的场景,其中有一个如下所示的模块:
def fun2():
#does something which can throw a ValueError exception
def fun3():
#does something which can throw a ValueError exception
def fun1:
fun2() #call to fun2
fun3() #call to fun3
def fun0:
try:
fun1()
except ValueError as e:
##try to find out from which function ValueError Exception is thrown
print(customErrorMsg)
我如何找出在fun0
的except块中,从fun2
或fun3
引发了错误?我尝试了e.__traceback__
,但没有给出有用的输出。
严格来说,当从customErrorMsg
或fun2
抛出异常时,我想打印不同的fun3
。
答案 0 :(得分:0)
您可以这样做
def fun1:
try:
fun2() #call to fun2
except ValueError as e:
print(error_message)
try:
fun3() #call to fun3
except ValueError as e:
print(error_message)