0. Description
If you want to write the user experience high code, then you need to take into account in the execution of their own written code in the process of interacting with the user may occur, that is, the need to deal with the possible exceptions, only do the work to write the user experience good code.
1. What is an exception
The error is on the syntax (which causes the interpreter to be unexplained) or logic (that is, the code quality problem), in Python, when an error is detected, the interpreter indicates that the current stream cannot continue, and an exception occurs.
An error occurred in the program and the behavior taken outside of the normal control flow.
As explained above, it can be understood that an exception is triggered whenever the interpreter detects that an error occurred while the program was running, which is incompatible with the Python interpreter.
Exceptions in 2.Python
As follows:
exception type |
|
Simple example |
nameerror |
Try to access an undeclared variable, or a variable that does not exist in the namespace |
>>> xpleaftraceback (most recent call last): file "<stdin>", line 1, in <module>NameError: name ' xpleaf ' is not defined |
zerodivisionerror |
divisor is zero |
>>> 1/0traceback (most recent call last): file "<stdin>", line 1, in < Module>zerodivisionerror: integer division or modulo by zero |
syntaxerror |
python interpreter syntax error Span style= "FONT-SIZE:14PX;" > (the only exception that is not at run time, which occurs at compile time, the Python interpreter cannot compile the associated script into a Python byte code) |
>>> for file " <stdin> ", line 1
for ^syntaxerror: invalid syntax |
indexerror |
requested index out of sequence range |
>>> alist = []>> > aList[0]Traceback (most recent call last): file "<stdin>", line 1, in <module>indexerror: list index out of range |
keyerror |
request a non-existent dictionary keyword |
>>> adict = {' name ': ' xpleaf ', ' love ': ' cl '}>>> adict[' Clyyh ']traceback (most recent Call last): file "<stdin>", line 1, in <module>keyerror: ' clyyh ' |
ioerror |
input/output error (any type of I/O error throws a IOError exception) |
>>> f = open (' Xpleaf ') Traceback (most recent call last): file "<stdin>", line 1, in < module>ioerror: [errno 2] no such file or directory: ' Xpleaf ' |
Attributeerror |
Attempting to access an unknown object property |
>>> class MyClass (object): ... pass ... >>> myinst = MyClass () >>> Myinst.nametraceback (Most RE Cent call last): File "<stdin>", line 1, in <module>attributeerror: ' MyClass ' object have no attribute ' name ' |
This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1762213
Python Review and Collation 8: Errors and exceptions