繁体   English   中英

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

[英]How to traceback the thrown error in python3.7?

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

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

你可以这样做

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM