I. Anomaly Basics
In order to increase the friendliness in the programming process, the error message will not be displayed to the user in the event of a bug in the program, but a prompt page of reality.
1) Common methods:
Try : Pass except Exception as E: Pass
Always try the code in try, if you encounter an error and the same exception in except, execute the code in except, if it is not the same as except, will throw an exception
#-*-coding:utf-8-*-__author__='Shisanjun'names=['1','2','3']data={}Try: #data[' name '] #Names[3] #Open ("1.txt")A=3except(Keyerror,indexerror) as E:Print("Dictionary Error", E)exceptIndexerror as E:Print("Index exceeds length", E)exceptFilenotfounderror as E:Print("something went wrong.")Else: Print("under normal conditions of execution")finally: Print("regardless of the error, the execution")
Always try the code in try, if you encounter an error and the same exception in except, execute the code in except.
Else If the other code is executed without encountering an exception, finally the following finally code is executed regardless of the exception
Second: Common anomalies
Attributeerror attempts to access a tree that does not have an object, such as foo.x, but Foo does not have a property xioerror input / output exception; it is basically impossible to open the file Importerror the module or package cannot be introduced is basically a path problem or name error Indentationerror syntax error (subclass); The code is not aligned correctly indexerror the subscript index exceeds the sequence boundary, for example, when X has only three elements, it attempts to access x[5]keyerror Attempting to access a key that does not exist in the dictionary keyboardinterrupt Ctrl+c is pressed Nameerror uses a variable that has not been given an object SyntaxError syntax error TypeError The incoming object type is incompatible with the required unboundlocalerror attempt to access a local variable that has not yet been set, basically because there is another global variable with the same name, causing you to think that you are accessing it valueerror pass in a value that the caller does not expect, even if the type of the value is correct
III: Other exceptions
Arithmeticerrorassertionerrorattributeerrorbaseexceptionbuffererrorbyteswarningdeprecationwarningenvironmenterroreoferror Exceptionfloatingpointerrorfuturewarninggeneratorexitimporterrorimportwarningindentationerrorindexerrorioerrorkeyboardint Erruptkeyerrorlookuperrormemoryerrornameerrornotimplementederroroserroroverflowerrorpendingdeprecationwarningreferenceerr Orruntimeerrorruntimewarningstandarderrorstopiterationsyntaxerrorsyntaxwarningsystemerrorsystemexittaberrortypeerrorunbou Ndlocalerrorunicodedecodeerrorunicodeencodeerrorunicodeerrorunicodetranslateerrorunicodewarninguserwarningvalueerrorwarni Ngzerodivisionerror
Four: Custom exceptions
Custom exceptions require an exception to be thrown manually: with raise, the sample code is as follows
#-*-coding:utf-8-*-__author__='Shisanjun'classalexexception (Exception):def __init__(self,msg): Self.msg=msgdef __str__(self):return "1111111"Try: RaiseAlexexception ("database connection does not")exceptalexexception as E:Print(e)
Python Basic Learning Log day8-exception handling