Python Getting Started Guide 4, Errors and exceptions

Source: Internet
Author: User
Tags throw exception

What is an exception? Errors detected during program operation are called exceptions.

We can catch and handle these exceptions through the program, which involves the try and raise statements

First look at the Try statement:

Execution mode:

    • First, execute a try clause
    • If there is no exception, the EXCEPT clause is ignored after the try
    • If an exception occurs when the TRY clause executes, then the remainder of the clause is ignored, and if the exception matches the specified exception after the except, the EXCEPT clause is executed and then executed down
    • If there is no except match, it will be uploaded to the previous try, if the processing statement can not be found, will become an unhandled exception, terminate the program run, display the prompt message
    • When the try clause does not throw an exception, and you need to execute some code, use the Else statement
    • Finally is executed in any case, even if the try clause exits the page through the Break,continue,return statement.

Attention:

    • At most only one except branch is executed

Second raise statement (throws an exception):

The raise statement is used to force the specified exception to occur, the first parameter specifies the name of the throw exception, and the second parameter that specifies the exception

Finally look at the exception parameter problem:

When an exception occurs, there may be a subordinate value, a parameter that exists as an exception, whether the parameter exists, what type it is, and what type of exception it depends on.

After the exception name, you can also specify a variable, which is bound to an exception instance, stored in the Instance.args parameter

Operation Result:

Python Getting Started Guide 4, Errors and exceptions

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.