Use of python—try-expect-else-finally

1 Introduction

In python, the try/except statement is mainly used to handle some abnormal situations that occur during the normal execution of the program, such as syntax errors (python is a scripting language without a compilation link, the syntax is checked during the execution process, and an exception is issued after an error Messages), data division by zero errors, values ​​on undefined variables, etc.; try/finally statements are mainly used in situations where some cleanup work is required regardless of whether an abnormal situation occurs , such as in the communication process, regardless of Whether there is an error in communication, you need to close the network connection when the communication is completed or an error occurs. Although the roles of try/except and try/finally are different, in programming practice they can usually be combined and used in the form of try/except/else/finally to achieve a more stable and flexible design.

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 responds to the exception message. The default processing is to terminate the execution of the program and print specific error messages. This is also the origin of the error printing message that appears after executing the program error in the python shell.

2.try expect format

python中try/except/else/finally语句的完整格式如下所示:
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")   
  • The normally executed program is executed in the Normal execution block execution block under try. If an exception occurs during execution, the current execution in the Normal execution block will be interrupted, and the execution will start in the corresponding exception handling block;
  • Python starts to search from the first except X, if it finds the corresponding exception type, it enters the exception handle provided by it for processing, and 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 the application and print a prompt message;
  • If no exception occurs during the execution of the Normal execution block, it will enter the else execution block (if it exists) after executing the Normal execution block.
  • Regardless of whether an exception occurs, as long as the finally statement is provided, the last step executed by the above try/except/else/finally code block will always execute the code block corresponding to the finally.

Note that:
1. In the complete statement shown above, the order of try/except/else/finally must be
try–>except X–>except–>else–>finally, that is, all except must be in else And before finally, else (if any) must be before finally, and except X must be before except. Otherwise, a syntax error will occur.
2. For the complete try/except format shown above, else and finally are optional, not necessary, but if it exists, e lse must be before finally, and finally (if it exists) must be in the entire statement The last position.
3. In the above complete statement, the existence of the else statement must be based on the except X or the except statement. If the else statement is used in a try block without an except statement, it will cause a syntax error. In other words, else cannot be used in conjunction with try/finally.

3.except-note

try:
     #raise AError
     asdas('123')
except AError:
     print("Get AError")
except:
     print("exception")      
else:
     print("else")
finally:
     print("finally")      
print("hello wolrd")
在上面的代码中,Normal execution block中出现了语法错误,但是由于使用了except语句,该语法错误就被掩盖掉了。
因此在使用try/except是最好还是要非常清楚的知道Normal execution block中有可能出现的异常类型以进行针对性的处理。

Guess you like

Origin blog.csdn.net/weixin_46649052/article/details/112852660