Python Exception handling

Source: Internet
Author: User

1. The program breaks execution at the place where the exception is thrown, and if it is not captured, it will terminate the program in advance raise statement raise nothing, indicating that the most recently activated exception is thrown, if there is no active exception, Then the raise of the thrown type exception is the subclass or instance of the Baseexception class, and if it is a class, it will be instantiated without a parameter.

Baseexception is the base class for all built-in exception classes

#sys the exception thrown by the. Exit () function, if the exception does not capture processing, simply hand over to the Python interpreter, and the interpreter exits import Systry:    sys.exit (1) except Systemexit:    print (' Exit ') Print (' outer ')

Exception and its sub-categories

Exception is the base class for all built-in, non-system-exited exceptions, and the custom exception should inherit from its SyntaxError syntax error, which Python also attributed to the exception subclass under the Exception class, but this error is not captured

Arithmeticerror

The exception that is thrown by all arithmetic calculations, except for the subclass of 0 exception

Lookuperror

The use of the mapped key or the epitome of the sequence is invalid is the base class of the exception thrown: Indexerror,keyerror

Custom exceptions

Inherit from Exception class
Capture principle, from small to large, from specific to Broad, caught not to throw an exception, if captured by a exception statement, the others will not catch the exception

As clause

The thrown exception should be an instance of the exception, using the AS clause to obtain the object
classmyexception (Exception):def __init__(self,code,message): Self.code=Code Self.message=messageTry:    RaiseMyException (' -','Error')exceptMyException as E:Print('{}-->{}'. Format (E.code,e.message))

Finally clause

Finally, finally, will be executed in the end, try ... The finally statement is executed regardless of whether an exception has occurred
The open function does not assign a value to F if the file is opened, if you do not give f=None Judge F will also throw Errorf=NoneTry: F= Open ('ABCDEFG')exceptException as E:Print('{}'. Format (e))finally:    Print("Clean Workstation")    iff:f.close ()#f.close ()

How the Try Works

1. If an exception occurs when the statement executes in try, search for the except clause, and executes the first except clause that matches the exception 2. If the execution of the statement in try does not have a matching except clause, the exception is submitted to the outer try, not processed, continues to be thrown, and the thread that terminates the exception is not processed 3. If no exception occurs when the try executes, the statement in the ELSE clause is executed 4 . The finally clause executes regardless of whether an exception occurs in a try

  

Python Exception handling

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.