python--Anomaly Hierarchy Analysis

Source: Internet
Author: User
Tags throw exception

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.