SoFunction
Updated on 2024-10-30

Python Exception Handling Mistakes Summary

Python Exception Handling Mechanism

If there is a finally in the try exception handling, the code in the finally will always be executed

The following examples are for demonstration purposes only, so don't bother with the business logic.

Easy Mistakes 1

Look at the code below and write the output

def dig_dig1():
    while True:
        print("I'm in while loop")
        try:
            print("I'm in try")
            raise EOFError
        except IOError:
            print("IOEoor")
        finally:
            print("I'm in finally")
            break


dig_dig1()

The result that was expected to be output:

I'm in while loop
I'm in try
I'm in finally
EOFError

Process finished with exit code 1

running result

I'm in while loop
I'm in try
I'm in finally

Process finished with exit code 0
  1. Finally will always be executed if there is a finally in the exception handling; the
  2. Then, before executing finally, the exception generated in try will be temporarily saved, and when the execution of finally's code completes, the exception will be thrown again.
  3. But when there is a raise or return or break in the finally, the exception in the try will be thrown.

Easy Mistake 2

Look at the code below and write the output

def dig_dig2(index):
    try:
        print("I'm in try")
        if index < 0:
            raise IndexError
        else:
            return index
    except IndexError:
        print("I'm in except")
        return "except"
    finally:
        print("I'm in finally")
        return "finally"


print(dig_dig2(12))

The result that was expected to be output:

I'm in try
12

running result

I'm in try
I'm in finally
finally

Process finished with exit code 0
  1. If finally is present in the exception handling, finally will always be executed.
  2. If there is a return in a try block statement and a finally block statement, then the finally block statement will be executed before the return statement in the try block statement is executed, and then the return statement in the try block statement will be executed again.
  3. But in the example, there is a return statement in the finally block statement, and the whole function has ended, so the return statement in the try block statement will never get executed.

summarize

In general, if there is a finally in the exception handling, finally will always be executed. However, the following situations require attention:

  1. Before executing the finally, the exception generated in the try will be saved temporarily and thrown when the execution of the finally code is completed; but when there is a raise or return or break in the finally, the exception in the try will be discarded.
  2. If there is a return in a try block statement and a finally block statement, then the finally block statement will be executed before the return statement in the try block statement is executed, and then the return statement in the try block statement will be executed again. However, if there is a return statement in the finally block statement, the entire function has ended, so the return statement in the try block statement will never be executed.

Above is Python exception handling easy to make mistakes in the summary of the details, more information about python exception handling please pay attention to my other related articles!