Basic syntax
In the programming process in order to increase the friendliness of the program in the event of a bug will not be displayed to the user error message, but the reality of a hint of the page, popular is not to let users see the big Yellow Pages!
Try : Pass except Exception as E: Pass
Requirement: Add two numbers entered by the user
whileTrue:num1= Raw_input ('NUM1:') num2= Raw_input ('num2:') Try: Num1=Int (NUM1) num2=Int (num2) result= Num1 +num2exceptException, E:Print 'An exception occurred with the following information:' PrintEView Codetype of Exception
There are so many exceptions in Python that each exception is dedicated to handling an exception!
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 use a variable that has not been assigned to the object SyntaxError Python code is illegal, the code cannot compile (personally think this is a syntax error, Wrong) TypeError the incoming object type is not compliant with the requirements Unboundlocalerror attempts to access a local variable that is not yet set, basically because another global variable with the same name causes you to think that you are accessing it valueerror a value that the caller does not expect , even if the type of the value is correct
Common Exceptions
Arithmeticerrorassertionerrorattributeerrorbaseexceptionbuffererrorbyteswarningdeprecationwarningenvironmenterroreoferror Exceptionfloatingpointerrorfuturewarninggeneratorexitimporterrorimportwarningindentationerrorindexerrorioerrorkeyboardint Erruptkeyerrorlookuperrormemoryerrornameerrornotimplementederroroserroroverflowerrorpendingdeprecationwarningreferenceerr Orruntimeerrorruntimewarningstandarderrorstopiterationsyntaxerrorsyntaxwarningsystemerrorsystemexittaberrortypeerrorunbou Ndlocalerrorunicodedecodeerrorunicodeencodeerrorunicodeerrorunicodetranslateerrorunicodewarninguserwarningvalueerrorwarni Ngzerodivisionerror
more Exceptionsexception Other Structure
Try : # main code block Pass except keyerror,e: # exception, the block is executed Pass Else : # when the main code block finishes executing, the block is executed Pass finally : # whether the exception or not, the final execution of the block Pass
proactively triggering exceptions
Try : # proactively triggering exceptions Raise Exception (' wrong ... ')except Exception as E: print E
Custom exceptions
classOldboyerror (Exception):#Defining Exception Classes def __init__(Self, msg): Self.message=msgdef __str__(self):returnSelf.messageTry: RaiseOldboyerror ('Error')#proactively triggering exceptionsexceptOldboyerror as E:Print(e)Assertion
# Assert Condition assert assert 1 = = 2
assert conditions, assertions, used to force the user to obey, do not obey the error, can be replenishment, generally do not replenish goods assert 1==1Print # normal printing 123assert 1==2print# Direct Error
View Code
Python basic syntax-exception handling