1. Standard exception
Assertionerror |
Assertion statement (assert) failed |
Attributeerror |
Attempting to access an unknown object property |
Eoferror |
User input file End flag EOF (CTRL+D) |
Floatingpointerror |
Floating-point calculation error |
Generatorexit |
When the Generator.close () method is called |
Importerror |
When the import module fails |
Indexerror |
Index out of range of sequence |
Keyerror |
Find a keyword that does not exist in the dictionary |
Keyboardinterrupt |
User input interrupt key (CTRL + C) |
Memoryerror |
Memory overflow (can free memory by deleting objects) |
Nameerror |
Try to access a non-existent variable |
Notimplementederror |
Methods that have not been implemented |
OSError |
An exception generated by the operating system (for example, opening a file that does not exist) |
Overflowerror |
Numeric operation exceeds maximum limit |
Referenceerror |
Weak references (weak reference) attempt to access an object that has been reclaimed by the garbage collection mechanism |
RuntimeError |
General run-time errors |
Stopiteration |
There are no more values for iterators |
SyntaxError |
Syntax error for Python |
Indentationerror |
Indentation Error |
Taberror |
Use the tab and Space mix |
Systemerror |
Python Compiler system error |
Systemexit |
Python compiler process is closed |
TypeError |
Invalid operations between different types |
Unboundlocalerror |
Access an uninitialized local variable (subclass of Nameerror) |
Unicodeerror |
Unicode-related errors (subclasses of ValueError) |
Unicodeencodeerror |
Error in Unicode encoding (subclass of Unicodeerror) |
Unicodedecodeerror |
Error in Unicode decoding (subclass of Unicodeerror) |
Unicodetranslateerror |
Error in Unicode conversion (subclass of Unicodeerror) |
ValueError |
Invalid parameter passed in |
Zerodivisionerror |
Divide by zero |
2. Hierarchy of built-in exception classes
Baseexception
+--Systemexit
+--Keyboardinterrupt
+--Generatorexit
+--Exception
+--stopiteration
+--Arithmeticerror
| +--Floatingpointerror
| +--Overflowerror
| +--Zerodivisionerror
+--Assertionerror
+--Attributeerror
+--Buffererror
+--Eoferror
+--Importerror
+--Lookuperror
| +--Indexerror
| +--Keyerror
+--Memoryerror
+--Nameerror
| +--Unboundlocalerror
+--OSError
| +--Blockingioerror
| +--Childprocesserror
| +--Connectionerror
| | +--Brokenpipeerror
| | +--connectionabortederror
| | +--connectionrefusederror
| | +--connectionreseterror
| +--Fileexistserror
| +--Filenotfounderror
| +--Interruptederror
| +--Isadirectoryerror
| +--Notadirectoryerror
| +--Permissionerror
| +--Processlookuperror
| +--Timeouterror
+--Referenceerror
+--RuntimeError
| +--Notimplementederror
+--SyntaxError
| +--Indentationerror
| +--Taberror
+--Systemerror
+--TypeError
+--ValueError
| +--Unicodeerror
| +--unicodedecodeerror
| +--unicodeencodeerror
| +--unicodetranslateerror
+--Warning
+--deprecationwarning
+--pendingdeprecationwarning
+--runtimewarning
+--syntaxwarning
+--userwarning
+--futurewarning
+--importwarning
+--unicodewarning
+--byteswarning
+--resourcewarning
3. Exception Handling 1.try statement
Try
Detection Range
Except Exception [as reason]:
Code to be processed after exception appears
You can have multiple except and try combinations, because the detection range can produce multiple exceptions, and you can use multiple except and try combinations to focus on the exceptions you are interested in.
It is possible to use () to enclose multiple exceptions that require the same processing after the same processing of multiple-class exceptions except.
Try: Int ('ABC') Sum= 1+'1'F= Open ('I'm a non-existent document. txt') Print(F.read ()) F.close ()except(OSError, Valueerror,typeerror) as reason:Print('Error t_t\n The cause of the error is:'+ str (reason))
2.try finally statement
Try
Detection Range
Except Exception [as reason]:
Code to be processed after exception appears
Finally
Be sure to do something (such as making a file close here after an error)
Try: F= Open ('My_file.txt')#The "my_file.txt" file does not exist in the current folder t_t Print(F.read ())exceptOSError as Reason:Print('Error:'+str (reason))finally: if 'F' inchLocals ():#If the file object variable exists in the current local variable symbol table, the open successF.close ()
3. Raise statement (throws an exception)
Raise Exception Name
Try : for in range (3): as in range (3): if i = = 2 : raise keyboardinterrupt print(i, j)except Keyboardinterrupt: print(' exit! ')
Python3 Anomaly Detection