Python Exception handling
- What is an exception type
-
- common types of anomalies
#common types of anomalies#Attributeerror attempts to access a tree that does not have an object, such as foo.x, but Foo has no attribute x#IOError input/output exception; Basically, the file cannot be opened#Importerror cannot introduce modules or packages; it is basically a path problem or a name error#indentationerror syntax error (subclass); Code not aligned correctly#indexerror Subscript Index is out of sequence boundary, for example, when X has only three elements, but tries to access X[5]#Keyerror attempts to access keys that do not exist in the dictionary#Keyboardinterrupt CTRL + C is pressed#Nameerror using a variable that has not been assigned to an object#syntaxerror python code is illegal, code can not compile (personally think this is a syntax error, write wrong)#TypeError Incoming object types are not compliant with the requirements#Unboundlocalerror attempts to access a local variable that has not yet been set, basically because another global variable with the same name causes you to think that you are accessing it#ValueError Pass in a value that is not expected by the caller, even if the value is of the correct type
Arithmeticerrorassertionerrorattributeerrorbaseexceptionbuffererrorbyteswarningdeprecationwarningenvironmenterroreoferror Exceptionfloatingpointerrorfuturewarninggeneratorexitimporterrorimportwarningindentationerrorindexerrorioerrorkeyboardint Erruptkeyerrorlookuperrormemoryerrornameerrornotimplementederroroserroroverflowerrorpendingdeprecationwarningreferenceerr Orruntimeerrorruntimewarningstandarderrorstopiterationsyntaxerrorsyntaxwarningsystemerrorsystemexittaberrortypeerrorunbou Ndlocalerrorunicodedecodeerrorunicodeencodeerrorunicodeerrorunicodetranslateerrorunicodewarninguserwarningvalueerrorwarni Ngzerodivisionerror
Extended Exception types
-
- Exception Handling Methods
- using the IF-judgement
Num1=input ('>>:')#Enter a string to tryInt (NUM1) num1=input ('>>:')#Enter a string to tryifnum1.isdigit (): Int (NUM1)#Our Orthodox program is here, and the rest belongs to the exception-handling category .elifnum1.isspace ():Print('Enter a space and execute my logic here.')elifLen (num1) = =0:Print('the input is empty, just execute my logic here.')Else: Print('other situations, execute my logic here.')" "Problem One: We only add exception handling for the first piece of code by using if, but these if are not related to your code logic, so your code will not be easy to read because of poor readability. This is just a small logic in our code, and if there is a lot of logic like that, Then every time we need to judge these things, we'll invert our code to be very verbose. " "
Python Exception handling