Python basic knowledge three - try and except handle exception statements

Introduction to try/except

Like other languages, in python, the try/except statement is mainly used to handle some exceptions that occur during the normal execution of the program, such as syntax errors (python, as a scripting language, is not compiled, and the syntax is detected during the execution process. , an exception message is issued after an error), data division by zero error, taking a value from an undefined variable, etc.; while the try/finally statement is mainly used in cases where some cleanup work needs to be performed regardless of whether an exception occurs, such as in During the communication process, regardless of whether an error occurs in the communication, it is necessary to close the network connection when the communication is completed or an error occurs. Although try/except and try/finally have different roles, they can usually be combined in the form of try/except/else/finally to achieve a more stable and flexible design in programming practice.


By default, during the execution of the program segment, if the try/except processing is not provided, the exception message generated during the execution of the script file will be automatically sent to the program caller, such as the python shell, and the python shell does not respond to the exception message. The default processing is to terminate the execution of the program and print the specific error message. This is also the origin of the error print message that appears after executing a program error in the python shell.
 

try/except format

The full format of a try/except/else/finally statement in python looks like this:
try:
     Normal execution block
except A:
     Exception A handle
except B:
     Exception B handle
except:
     Other exception handle
else:
     if no exception,get here
finally:
     print("finally")   
 
illustrate:
The normally executed program is executed in the execution block of the Normal execution block below the try. If an exception occurs during the execution process, the current execution in the Normal execution block will be interrupted the execution will be jumped to the corresponding exception handling block;
Python starts to search from the first except X. If it finds the corresponding exception type, it enters the provided exception handle for processing. If it does not find it, it directly enters the except block for processing. The except block is optional. If not provided, the exception will be submitted to python for default processing. The processing method is to terminate application and print a prompt message ;
If no exception occurs during the execution of the Normal execution block execution block, it will enter the else execution block (if any) for execution after the Normal execution block is executed.
Regardless of whether an exception occurs or not, as long as a finally statement is provided, the last step of the above try/except/else/finally code block always executes the code block corresponding to the finally.
 
have to be aware of is:
1. The order in which try/except/else/finally appears in the complete statement shown above must be try-->except X-->except-->else-->finally, that is, all except must be in else and Before finally , else (if any) must precede finally , and except X must precede except . Otherwise a syntax error will occur.
2. For the complete try/except format shown above, else and finally are optional, not required, but if present else must be before finally , finally (if present) must be in the entire statement last position .
3. In the above complete statement, the existence of the else statement must be premised on the except X or the except statement. If the else statement is used in the try block without the except statement, a syntax error will be caused . In other words , else cannot be used with try/finally .
4. The use of except should be very careful and used with caution.
class AError(Exception):
     """AError---exception"""
     print('AError')
 
try:
     #raise AError
     asdas('123')
except AError:
     print("Get AError")
except:
     print("exception")     
else:
     print("else")
finally:
     print("finally")     
print("hello wolrd")
In the above code, there is a syntax error in the Normal execution block, but the syntax error is masked due to the use of the except statement. Therefore, it is best to use try/except to know very clearly the types of exceptions that may occur in the Normal execution block for targeted processing.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324880605&siteId=291194637