Takkun Takkun - 9 days ago 5
Python Question

How far up the chain of function calls does an exception travel?

For example, say

func1
calls
func2
, which calls
func3
, which calls
func4
, which raises an exception.

If I put a try/except when calling
func1
, will it catch the exception raised by
func4
?

func1 --> func2 --> func3 --> func4 Raises exception

Answer

Exceptions propagate until they reach a handler, or until they reach the entry point for the program or thread. If the latter happens, your program will terminate. So, yes, the exception will be caught in func1, unless one of the other functions catches it first.

Comments