The try/except/else/finally statement in Python

Source: Internet
Author: User

As in other languages, in Python, thetry/except statement is primarily used to handle some exceptions that occur during normal execution of a program , such as syntax errors (Python is not compiled as a scripting language, and syntax is detected during execution. Exception message after an error), data except 0 errors, values that have never been defined, and so on, while the try/finally statement is mainly used to perform some cleanup work whether or not an exception occurs , such as during communication, regardless of whether the communication error occurred, Need to shut down the network connection when communication is complete or an error occurs. Although the roles of try/except and try/finally are different, they can often be combined in programming practice to achieve a better design of stability and flexibility using the form of try/except/else/finally.

By default, during the execution of a program segment, if no try/except processing is provided, the exception messages generated during script file execution are automatically sent to the program's caller, such as the Python shell, and python The shell's default handling of exception messages is to terminate the execution of the program and print specific error messages. This is also the origin of the error printing information that occurs after executing a program error in the Python shell. The full format of the try/except/else/finally statement in Python is as follows: Try:normal execution blockexcept A:
Exception A handleexcept b:exception B handleexcept:other Exception handleelse:
If no exception,get herefinally:
Print ("finally") Description: The normal execution of the program is executed under the normal execution block execution blocks under try, and if an exception occurs during execution, the interrupt is currently in normal execution Execution jumps to the corresponding exception handling block in the block, and Python starts at the first except X, and if the corresponding exception type is found, it is processed in the exception handle it provides. If not found then directly into the except block for processing. The except block is optional, and if not provided, the exception will be submitted to Python for default processing by terminating the application and printing a hint, if no exception occurs during the execution of the normal execution block. After the normal execution block is executed, it goes into the else execution block, if one exists. The last step in the execution of the above try/except/else/finally code block always executes the code block for finally, regardless of whether an exception occurred or not, as long as the finally statement is provided. It should be noted that: 1. The order in which the try/except/else/finally appears in the complete statement shown above must be try-->except x-->except-->else-->finally, That is, all except must precede else and finally, else (if any) must precede finally, and except x must precede except. Otherwise, a syntax error will occur. 2. For the try/except full format shown above, else and finally are optional, not mandatory, but if there is, else must precede finally, and finally (if present) must be in the last position of the entire statement. 3. In the complete statement above, the presence of the Else statement must be premised on the except X or except statement, and a syntax error is raised if the Else statement is used in a try block without a except statement. That is, else cannot be used in conjunction with try/finally. The use of 4.except should be very careful, use 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 code above, there was a syntax error in the Normal execution block, but the syntax error was masked by the use of the except statement. Therefore, it is best to be aware of the possible exception types in the normal execution block for targeted processing when using try/except.

The try/except/else/finally statement in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.