python notes --try except method

A common mistake summary
AttributeError no attempt to access an object tree, such as foo.x, but foo does not attribute the X-
IOError input / output abnormalities; basically can not open the file
ImportError not import module or package; basically a question or path name error
IndentationError syntax error (subclass); the code is not properly aligned
IndexError subscript index out of sequence boundaries, such as when only three elements of x, is trying to access x [. 5]
a KeyError attempt to access the key does not exist in the dictionary
KeyboardInterrupt Ctrl + C is pressed
NameError use a variable has not been given an object
SyntaxError Python code is illegal, the code does not compile (personally think it's a syntax error, wrong)
TypeError incoming object type does not comply with the requirements of
UnboundLocalError trying to access has not been a local variables are set, basically due to a global variable otherwise the same name,
cause you think it is being accessed
ValueError incoming caller does not expect a value, even if the value is correct type of
two, try except method
sometimes we written procedures, there will be Some errors or abnormal, resulting in termination of the program, this time we need to capture the type of error, make the code more flexible, described below under the common try except to catch exceptions methods.

Processing a single exception:
Syntax:
>>> the try:
>>> code
>>> the except Error1 AS E: # exception processing Error1
>>> Print (E)
. 1
2
. 3
. 4
Demo
>>> the try:
>>> Print (. 5 / 0) # test the behavior statement
>>> except ZeroDivisionError as e: ## if the exception (in the ZeroDivisionError embodiment) except the test statement appears, the next line of code, otherwise skip this module
>>> print (e) ## to print the name of the error
division by zero ## the execution result
. 1
2
. 3
. 4
. 5
separately handling multiple exceptions:
syntax
>>> the try:
>>> code
>>> the except Error1 AS E: # exception processing Error1
>> > Print (E)
>>> the except Error2 AS E: # exception processing Error2
>>>print (e)
1
2
3
4
5
6
demo
A = >>> [0, 1]
>>> the try:
>>> Print (A [3])
>>> Print (5/0) ## statement on his party encountered an error spread except to go inside , this statement is not detected
>>> except ZeroDivisionError as e: # exception processing Error1
>>> Print (E)
>>> the except IndexError AS E: # exception processing Error2
>>> Print (E)
List of Range index OUT
. 1
2
3
4
5
6
7
8
9
unified handling multiple exceptions:
syntax
>>> the try:
>>> code
>>> the except (Error1, Error): ### whether anything unusual encounter, the next line will only be printing a thing
>>> Print (E)
. 1
2
. 3
. 4
no abnormality, the else part take logic code
syntax:
>>> the try:
>>>code
>>> except (Error1,Error2,...) as e:
>>> print(e)
The else >>>:
>>> Print ( "no error, execution")
. 1
2
. 3
. 4
. 5
. 6
Demo
>>> the try:
>>> Print (5/1)
>>> the except the ZeroDivisionError AS E:
>>> Print (E)
>>> the else:
>>> Print ( "no error"),
no error
. 1
2
. 3
. 4
. 5
. 6
. 7
whether or not an error code will be executed finnally
example, the file operation when the file read if error occurred during, can not be closed, you can f.close (http://www.amjmh.com/v/) finally put in, so regardless of whether there is an error, the file will be closed
>>> the try:
>>> code
the except >>> (Error1, Error2, ...) AS E:
>>> Print (E)
>>> the else:
>>> Print ( "Is correct, run ")
>>> finnally:
>>> Print (" Whether there is nothing wrong, perform finnally ")
---------------------

Guess you like

Origin www.cnblogs.com/hyhy904/p/11322441.html