简体   繁体   English

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

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

I have a typical scenario where I have a module like below: 我有一个典型的场景,其中有一个如下所示的模块:

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)

How can I find out that in the except block of fun0 , error is thrown from fun2 or fun3 ? 我如何找出在fun0的除外块中,从fun2fun3引发了错误? I tried e.__traceback__ but it doesnt give useful output. 我尝试了e.__traceback__但是它没有提供有用的输出。 Strictly speaking, I want to print different customErrorMsg when the exception is thrown from fun2 or fun3 . 严格来说,当从fun2fun3抛出异常时,我想打印不同的customErrorMsg

You could do this 你可以这样做

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