Ask coding questions

← Back to all posts
Why is a function catching an exception from another function that was called?
JohnWang12 (1)

So, when function four is called it will lead to an exception (if you enter "e"), but the exception handler is in function one (function one wasn't called!) and somehow it gets called? Someone plz explain what is happening here

Comments
hotnewtop
[deleted]

What do you mean?

JohnWang12 (1)

@Tercoder so, function one is defined with an exception handler, but an exception from calling function four gets handled from the handler in function one (function two tries to make its parameter into an integer so it will throw an exception if it is handed a letter. Function four calls function three which asks you for something and if you enter a letter it will get passed down to function two. It will then throw an exception, but the exception will then get handled by function two. I'm confused on why an exception handler from another function is catching function four's exception).

[deleted]

@JohnWang12 Is this a function to check if a number (int)

JohnWang12 (1)

@Tercoder no, it tries to turn whatever it receives into an int

slickassassin03 (99)

@JohnWang12

def to_num(in):
    try:
        int(in)
    except ValueError:
        print("Must be numerical")

to_num(input(">>> ")