[Python] python try Exception Handling Mechanism, python exception handling

Source: Internet
Author: User

[Python] python try Exception Handling Mechanism, python exception handling

# There are two types of try statements in python # One is to handle exceptions (try/try t/else) # Two: whether an exception occurs or not, the final code will be executed (try/finally) try/release T/else style try: <Statement> # Run another code release T <Name>: <Statement> # If a 'name' exception occurs in the try part, <name>, <data >:< Statement> # If a 'name' exception is thrown, obtain the additional data else: <Statement> # If no exception occurs # try works, when a try statement is started, python marks it in the context of the current program, in this way, when an exception occurs, you can return here. The try clause is executed first, and what will happen next depends on whether an exception occurs during execution. #1. If an exception occurs when executing the try statement, python will jump back to try and execute the first limit t clause that matches the exception. The Exception Processing is complete, the control flow uses the entire try Statement (unless a new exception is thrown when an exception is handled ). #2. If an exception occurs in the statement after try, but there is no matching limit t clause, the exception will be submitted to the try at the upper layer, or to the top of the Program (this will end the program and print the default error information ). #3. If no exception occurs during the execution of the try clause, python will execute the statement after the else Statement (if else exists) and then control the flow through the entire try statement. Try/finally style try: <Statement> finally: <Statement> # When you exit try, python will always execute the finally clause, regardless of whether an exception is thrown when the try clause is executed. 1. If no exception occurs, python runs the try clause, followed by the finally clause, and then continues. 2. If an exception occurs in the try clause, python will return to execute the finally clause and submit the exception to the upper try. The control flow will not pass the entire try statement. Try/finally is useful when you want to make sure that some code is executed regardless of whether an exception occurs. # This is useful when opening a file. finally is always in the form of a try statement clause in the finally close () file. table listing T: Catch all exceptions. Alias t name: only capture specific exceptions. Alias t name, value: capture exceptions and its additional data (save exception information to value,) except T (name1, name2): Catch any listed exceptions else: If no exception exists finally: always execute >>> try: f = open('file.txt ') handle T IOError, e: print eelse: print 'wrong' [Errno 2] No such file or directory: 'file.txt 'the latest python version supports try/try t/finallytry: 1: If x is not abnormal, run z, I x 2: If x is abnormal, 1: if the cause t captures an exception, execute y, istrap T (name): 2: not captured, execute I, and then return the built-in Exception Processing yelse: zfinally: I

 

Related Article

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.