Python in Try ... except

Source: Internet
Author: User

Catching exceptions

Try: The code snippet below is the code snippet that needs to catch the exception;

Except: Catch the exception of a module, with the exception module name, with the reason parameter, except code for the exception occurs when the code executed, a try can correspond to multiple except clauses.

Note: Python 2 and 3 are handled differently on this side of the exception because the Python2 statement is:, followed by the reason variable name, and the Python3 statement is the exception module name as reason variable name

Other

Else: only after all except clauses, and only one, whose statement is the code that executes when no exception occurs for all except clauses (if there are other exceptions, an error is not performed)

Finally: Whether the exception occurs or does not occur, capturing or not capturing code snippets that are executed; Finally, you can define cleanup behavior in real-world scenarios where the finally clause is used to free external resources (such as file or network connections). Regardless of whether or not they were used in the process of error.

Example

Try:    print (1/0) except Zerodivisionerror as err:        # divisor is 0 error    print (' Exception: ', err) except Arithmeticerror As err:          # arithmetic error    print (' Exception: ', err) except StandardError as err:            # All built-in standard exception base class contains the above class    print (' except Ion: ', err) except Exception as err:                # General error base class    print (' Exception: ', err) except baseexception as err:            # All Exception base Class    print (' Exception: ', err) Else:    print ("No Errors") Finally:    print ("No matter what, it'll always is her E. ")

  

Python in Try ... except

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.