0

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? I tried e.__traceback__ but it doesnt give useful output. Strictly speaking, I want to print different customErrorMsg when the exception is thrown from fun2 or fun3.

Anudocs
  • 686
  • 1
  • 13
  • 54
  • can you show an example of the full traceback you are getting now? – WiseDev Jul 05 '19 at 09:32
  • I am getting when i used e.__traceback__ – Anudocs Jul 05 '19 at 09:32
  • I mean if you simply do `print(e)` in your `except ValueError as e:` statement – WiseDev Jul 05 '19 at 09:34
  • 2
    One way to do it is to modify your `fun1` and try both statements `fun2()` and `fun3()` seperately? – WiseDev Jul 05 '19 at 09:37
  • https://stackoverflow.com/questions/4564559/get-exception-description-and-stack-trace-which-caused-an-exception-all-as-a-st – RafalS Jul 05 '19 at 09:43
  • could you explain why this would ever be a useful thing to do? seems like you should either do something like @AdForte is suggesting or explicitly add something to the exception that you can use to determine the source. otherwise you're encouraging strong coupling between these components in ways that are less than obvious – Sam Mason Jul 05 '19 at 10:32
  • This is already written code by someone else. I am just trying to debug. I just want to find out the function name which has thrown the exception. – Anudocs Jul 05 '19 at 12:31

1 Answers1

0

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)
AdForte
  • 305
  • 2
  • 12