Detailed explanation of Python exception handling mechanism structure

First, Python's complete exception handling syntax structure is as follows:

the try :
     # service implementation code for 
the except Exception1 AS E:
     # exception handling block. 1 
    ...
 the except Exception2 AS E:
     # exception handling block 2 
    ...
 # may have a plurality of the except 
...
 the else :
     # normal processing blocks 
the finally :
     # Recycling blocks 
    ...

The execution process of the entire exception handling structure is shown in Figure 1.

 

Note that in the entire exception handling structure, only the try block is required, that is to say:

  • If there is no try block, there cannot be the following except block, else block and finally block. But you can't just use try block, either try except structure or try finally structure;
  • The except block, else block, and finally block are all optional, but they can also appear at the same time;
  • There can be multiple except blocks, but the except block that catches the exception of the parent class should be located behind the except block that catches the exception of the subclass;
  • Multiple except blocks must be placed after the try block, and finally blocks must be placed after all except blocks.
  • To use the else block, try and except must be included in front of it.


Among them, many beginners can't tell the difference between finally and else, and I will focus on it here. The else statement block will be executed only when no exception occurs, while the finally statement will be executed regardless of whether an exception occurs. Not only that, whether it is a normal exit, an abnormal exit, or a break, continue, or return statement, the finally block will be executed.

Note that if a statement that forces the Python interpreter to exit (such as os._exit (1)) is run in the program, the finally statement will not be executed. E.g:

import os
 try : 
    os._exit ( 1 )
 finally :
     print ( " Execute finally statement " )

Run the program without any output. Therefore, unless the method of exiting the Python interpreter is called in the try block or except block, no matter what kind of code is executed in the try block and except block, whatever happens, the finally block of exception handling will always be executed.

In addition, under normal circumstances, do not use statements such as return or raise in the finally block that cause the method to stop (raise statement will be described later). Once the return or raise statement is used in the finally block, it will cause a try block, except The return and raise statements in the block are invalid. See the following procedure:

DEF the Test ():
     the try :
         # because finally block contains a return statement 
        # so the following statement to return useless 
        return True
     finally :
         return False
 Print (the Test ())

The above program defines a return False statement in the finally block, which will cause the return true in the try block to be useless. Running the above program, the output is:

False

Similarly, if the Python program contains a return or raise statement in the try block or except block, the Python interpreter will first find the finally block when it executes the statement. If there is no finally block, the program will immediately execute the return or raise statement ; Conversely, if a finally block is found, the system immediately starts to execute the finally block. Only when the finally block is executed will the system jump back to execute the return or raise statement in the try block or except block.

However, if statements such as return or raise are used in the finally block, which causes the method to abort, the finally block has already aborted the method, and the system will not jump back to execute any code in the try block or except block.

Try to avoid using return or raise statements in the finally block that cause the method to abort, otherwise some very strange situations may occur.

Guess you like

Origin www.cnblogs.com/bashliuhe/p/12749270.html