Exception handling basic syntax:
try: pass except Exception,ex: pass
Common Error type:
AttributeError attempts to access an object, not the property. For example foo.x, but no attribute foo X IOError input / output is abnormal (substantially is unable to open the file) ImportError package or module can not be introduced into the (substantially name or path error problem) one kind IndentationError syntax errors, the code does not indented IndexError subscript index out of sequence boundaries, such as a list of only three elements x, is trying to access x [ . 5 ] a KeyError attempts to access the dictionary does not exist in the bond the KeyboardInterrupt the Ctrl + C is pressed variable NameError use a yet undefined SyntaxError Python code is illegal, the code does not compile TypeError incoming object type does not comply with the requirements of UnboundLocalError attempt to access a local variable has not been set, basically due to a global variable otherwise the same name leads you to think that it is being accessed ValueError pass a the caller does not expect value, even if the type of value is correct. For example, a character string you want to convert to digital: n- = ' ABC ' int (n-)
Single abnormal crawl:
= {Data ' ID ' : 123 } the try : Data [ ' name ' ] the except a KeyError AS E: Print ( ' no such Key: ' , E)
Multiple exceptions crawl:
In this way if the first wrong, it will not perform exception processing behind.
= {Data ' ID ' : 123 } List = [1,2 ] # processes only KeyError, IndexError not be processed later. the try : Data [ ' name ' ] List [ . 3 ] the except a KeyError AS E: Print ( ' no such Key: ' , E) the except IndexError AS E: Print ( ' list operation error: ' , E)
More of the specified exceptions, returns the same result:
# More specified exceptions, returns the same result. # As long as there have been any one of which would be returned. = {Data ' ID ' : 123 } List = [1,2 ] the try : Data [ ' name ' ] List [ . 3 ] the except (IndexError, a KeyError) AS E: # when specified error occurred, execution. Print ( ' emerged specified error. ' )
Universal Exception:
No matter what mistakes, can obtain. Not recommended to start is to use.
= {Data ' ID ' : 123 } List = [1,2 ] the try : Data [ ' name ' ] List [ . 3 ] the except Exception AS E: # fetch any abnormality Print ( " error occurred. ' )
Other structural abnormalities:
When except Exception unspecified error, execution.
When else is correct, run
finally Whether there is nothing wrong, it is executed.
= {Data ' ID ' : 123 } List = [1,2 ] the try : # Data [ 'name'] List [ ' ABC ' ] n- . 1 = the except (IndexError, a KeyError) AS E: # when specified error occurred, carried out. Print ( ' occurred specified error. ' ) the except Exception AS E: # when a non-specified error occurred, execution. Print ( ' there was an unspecified error Unknown error! ' , E) the else : # no error, execution. Print ( 'Everything is normal, no errors. ' ) A finally : # Whether there is nothing wrong, it is executed. Print ( ' Whether there is nothing wrong with the implementation of all this! ' )
Active trigger an exception:
the try : The raise Exception ( ' active trigger exception! ' ) the except Exception AS E: Print (E)
Custom exception:
class MyExceptin (Exception): DEF the __init__ (Self, MSG): self.msg = MSG the try : The raise MyExceptin ( ' custom exception! ' ) the except MyExceptin AS E: Print (E)