1 , most of the built-in exceptions are exception subclass
2, exception itself inherits from the Baseexception class
3, so all exceptions must be extended from the Baseexception class
4, Exception is a subclass of the Baseexception class, and Nameerror is a subclass of exception, Nameerror is a subclass of baseexception class (or indirect subclass)
5. Is there an exception class that inherits directly from the Baseexception class? Yes, that's the following two
systemexit:
This exception is thrown by the rule that the Systemexit exception will be triggered regardless of whether the program exits normally
Then the exit action, usually after calling Sys.exit () in a piece of code, triggers the systemexit exception
systemexit exception exists in order to clean up the code before the program exits, but does not need to be displayed to handle it.
The action of the cleanup code is best placed in the finally in Try-finally, the finally statement is executed whether or not an exception will be performed
Keyboardinterrupt:
This exception applies to the command-line program, and his trigger rule is to press CTRL + C on the keyboard, at which point the exception is thrown
6, about Sys.exit ()
sys.exit () throws a Systemexit exception, and if this exception is not caught
The python interpreter exits directly; Catching this exception can do some extra cleanup work.
0 for normal exit, other values (1-127) are not normal, can throw exception events for capture.
7. See if other exception classes are really inherited from exception
If you view Nameerror
8. View the parent class of Systemexit and Keyboradinterrupt
9. Abnormal level diagram
10, it is important to note that
If you just use the except: statement without specifying any type of exception, all baseexception subclasses will be captured
Try:passexcept:pass
That is, if you do this, you will catch all exceptions except Systemexit and Keyboradinterrupt, which are not recommended.
If you need to catch any exceptions (except those two special exceptions) the recommended practice is to indicate that it is except exxception, see the following code
Try:passexcept Exception:pass
Conversely, if you do not want to catch all exceptions, then indicate Baseexception, the code is as follows
Try:passexcept baseexception:pass# So, it's just dealing with the two special anomalies, so it's not catching exception.
python--Anomaly Hierarchy Analysis