SoFunction
Updated on 2024-11-19

Exception propagation in python explained in detail

1. Spread of anomalies

When an exception occurs in a function, if the exception is handled in the function, the exception does not continue to propagate. If the exception is not handled in the function, the exception continues to propagate to the function caller. If the function caller handles the exception, it is not propagated anymore, if it is not handled, it continues to be propagated to his caller until it is passed to the global scope (the main module) If it is still not handled, the program terminates and an exception message is displayed to the console. So the propagation of an exception is also called throwing an exception.

An example of exception propagation is shown below:

def fn1():
    print('Hello fn')
    print(10/0)

def fn2():
    print('Hello fn2')
    fn()

def fn3():
    print('Hello fn3')
    fn2()

fn3()

Output results:

Describe the output results:

When we call thefn3()method will first output theHello fn3and then execute the call down the line.fn2()method, and so on to thefn1()method. You can see that there are three output statements on the top.

When executing thefn1()methodologiesprint(10/0)statement that appears in theZeroDivisionErrorabnormalities, and thefn1()method does not handle the exception, it is thrown to his caller, to thefn2()method, and so on, knowing thatfn3()method is thrown to the caller in the global scope, the exception remains unhandled.

Finally Python's interpreter displays the exception in the console.

So we see a line in the exception result that reports an error and throws upwards several times.

For example, code in a global scope with an exception that is not handled is displayed directly on the console.

print(10 / 0)

Output results:

2、How to deal with exceptions

Any caller that handles the exception after it is thrown is fine.

The exception is handled as follows:

def fn1():
    print('Hello fn')
    print(10 / 0)

def fn2():
    print('Hello fn2')
    fn1()

def fn3():
    print('Hello fn3')
    fn2()

try:
    fn3()
except :
    print("Anomalies I've already dealt with.")
"""
Output results:
Hello fn3
Hello fn2
Hello fn
I've handled the exception.
"""
# I've got exception handling where the global scope is. #
# It can also be processed elsewhere, wherever that may be. #
# It won't throw an error to the console at the end of the day。

Tip:

When an exception occurs during program execution, all the exception information is saved in a specialized exception object. When an exception is propagated, the exception object is actually thrown to the caller.

And different errors will correspond to different exception objects (i.e., the previously mentioned exception types).

How to view Python exception objects?

In Python's native documentation, find [The Python Standard Library] -> [Built-in Exceptions], which is full of exception objects and descriptions of what we have in Python.

As shown in the figure below:

to this article on the propagation of exceptions in python detailed article is introduced to this, more related python exception propagation content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!