Python Errors and exceptions

Source: Internet
Author: User

Error

It is divided into grammatical and logical errors, as follows:

Syntax errors indicate that there is a structural error on the software that cannot be interpreted by the interpreter or compiled by the compiler.

A logic error may be caused by incomplete or illegal input, or a process that cannot be generated, calculated, or required to output the result.

Abnormal

When Python detects an error, the interpreter indicates that the current stream has failed to continue. There is an exception.

Description: An exception is a behavior that is taken outside of the normal control flow because of an error in the program. The behavior is divided into two stages:

The first stage: An error that causes an exception to occur, and occurs after an exception condition has occurred. The interpreter throws an exception whenever an error is detected and the exception condition is realized;

Phase One: detection and adoption of possible steps.

Exceptions in Python

1. Nameerror: Try to access an undeclared variable

2, Zerodivisionerror: Divisor is zero

3. SyntaxError: Interpreter syntax error

4. Indexerror: The requested index is out of sequence range

5, Keyerror: Request a non-existent dictionary key word

6. IOError: input/Output error

7. Attributeerror: Attempt to access unknown object properties

Detecting and Handling exceptions

There are two main forms of a try statement:

Try-except and try-finally.

Try
Try_suite #监控这里的异常

Except exception[, reason]:

Except_suite #异常处理代码

Finally clause

The finally clause is a piece of code that will be executed regardless of whether the exception occurred or not.

Try

A

Except MyException:

B

Else

C

Finally

D

Exception execution: a-b-d no exception execution a-c-d

Python Errors and exceptions

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.