如何追溯python3.7中引发的错误?

时间:2019-07-05 09:30:21

标签: python error-handling try-catch try-except

我有一个典型的场景,其中有一个如下所示的模块:

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块中,从fun2fun3引发了错误?我尝试了e.__traceback__,但没有给出有用的输出。 严格来说,当从customErrorMsgfun2抛出异常时,我想打印不同的fun3

1 个答案:

答案 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)