When can the error be caught (in functions)?

If method A calls B, and B calls C, and then an error is raised in function C, where can it potentially be caught (A, B, C, or all of the above)?

Answer

You could write a short code and try it yourself. Something like this:

def func1(a):  #will call func2
    print("in first func #1")
    x=a^2
    func2(x)
    print("in first func #2")
def func2(b):  #will call func3
    print("in second func #1")
    func3(b*2)
    print("in second func #2")
def func3(c):  #function with error
    print("in third func #1")
    print(c+ "str")  #can't use + operator on str and int type
    print("in third func #2")
a=5
func1(a)

You’ll end up with this error:

in first func #1
in second func #1
in third func #1
Traceback (most recent call last):
  File "--", line 15, in <module>
    func1(a)
  File "--", line 4, in func1
    func2(x)
  File "--", line 8, in func2
    func3(b*2)
  File "--", line 12, in func3
    print(c+ "str")  #can't use + operator on str and int type
TypeError: unsupported operand type(s) for +: 'int' and 'str'

It can be caught in func3

Leave a Reply

Your email address will not be published. Required fields are marked *