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