“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

Part ONE: : : Exception

Note: When an exception occurs in the code, the rest of the code does not execute. Exceptions themselves are classes

Fact: All exceptions in Python inherit from BaseException

It falls directly into four categories: SystemExit: Python exit exceptions KeyboardInterrupt: Python exit exceptions (Ctrl+C) GeneratorExit: GeneratorExit exceptions: normal exceptions (exceptions that will only be used in this part)

Part TWO: : : Exception handling

(1) Basic try… except…

For example:

Try: print(11) # print(a) # print(a) # print(a) # print(a) # print(b) # print(b) # print(b) # print(b) # print(b) # print(c) # print(b) # print(c) # print(b) # print(c) # print(b) # print(c) # print(b) # print(c) # print(c) # print(c) # print(c) # print(d) # print(c) # print(d) # print(c) # print(d)Copy the code

Output: 11 There is an exception

Note: except must be followed by try

(2) Catch specific exceptions

Except can be used to catch the specific exception type and as can be used to store the caught exception information in the following variable

Example:

Try: print(11) print(a) print(22) except NameError as t: Print (' This is an Exception ') print(' error: %s'%t ')Copy the code

Error: name ‘a’ is not defined

(3) Catch a variety of exceptions

Except (except

try:
        pass
except TabError:
        pass
except NameError:
        pass
Copy the code

Use parentheses after except to enclose various types of exceptions

try:
        pass
except (NameError,TabError):
        pass
Copy the code

If not sure of the exception type:

Try: pass except Exception: # because Exception contains all normal Exception passesCopy the code

Exception and its subclasses: All exceptions that occur in code are subclasses of Exception, so in except you only need to add Exception at the end

In the process of catching an exception, it will fall down from the top and compare the exception in turn. Once it is found, it will not look back

(4) Richer structure:

Try: print(11) print(a) print(22) except Exception as result: print(' catch Exception, will execute the following code to handle Exception ') print(result) else: Print (' no exception caught, execute me ') finally: print(' Execute with or without exception ')Copy the code

Note: 1. Put the code under the try that may fail. 2. Except the code under the except try that is executed when an error occurs

(5) Extension (custom exception types)

Raise Actively raises an exception format: raise Exception type Note: Raise is the exception type written after it is actively raised

1. Write your own custom exception:

Class WuMou(Exception): pass raise WuMou(' error ')Copy the code

The output is:

Traceback (most recent call last): File "C:/my/pycharm_work/ceshi.py", line 3, in <module> raise WuMou(' error ') __main__Copy the code

You can catch this exception:

Class WuMou(Exception): pass try: raise WuMou(' error ') except WuMou as h: print(h)Copy the code

The output is: error occurred

2. Examples:

Raise NameError(' There is an error ')Copy the code

The output is:

Traceback (most recent call last): File "C:/my/pycharm_work/ceshi.py", line 2, in <module> raise NameError(' error ') NameError: ErrorCopy the code