python12. exception

Exception: (itself is a class, an exception is an error)
BaseException is the parent class of all exceptions, including the following
SystemExit: Python exit exception
KeyboardInterrupt: keyboard interrupt (Ctrl+C, directly interrupt the loop)
GeneratorExit: generator exit exception
Exception: normal Exception (only this part of the exception will be used)

Exception'handling':
try:
print (code that will report an error)
except error type with error code: add an as xxx to the variable to save the caught exception, and only if there is an error in the try caught by as, can it be printed. The following variables get the abnormal return value.
Handling method. . . In this way, no error will be reported.
A richer structure can be added with else (execute without error) and finally (can be used for file closing) structure

When multiple exceptions are caught, the level is changed from small to large, so the small is at the top, and the big is below the code, otherwise the subsequent code will not
be used. The exceptions that will appear in the code are all subclasses of Exception, so in the except You only need to add Exception at the end (so that multiple exceptions can be tracked, but it is not accurate enough for which type of error).
In the process of capturing exceptions, the exceptions will be compared from top to bottom, and after they are found, they will not go any further. Look after

raise is to actively throw the exception type written later, format: raise exception type, you can actively throw a custom exception (the newly created exception is not a built-in exception type, so you must inherit Exception) when it
is saved in a variable by except, what is printed is Blank line, raise is placed after the exception handling, and it prints the thrown exception.
Custom exception type:
1. Add raise and throw
2. Find the error and backtrack the error. (1) Look at the last line, and then look from top to bottom.
Error view (2) Use dichotomy
(3) Look at the error details
(4) If you find an error in the function, the error report details are from the nearest to the farthest

Assertions (custom speech) statements are a convenient way to insert debugging assertions into programs

The syntax rules of assert are:

The expression returns True without reporting an error and then output the printed content

The expression returns False, an error is reported and then AssertionError is printed

Guess you like

Origin blog.csdn.net/qwe863226687/article/details/113969105
Recommended