To understand the usage of try except exception handling, simply speaking, when an exception occurs during the execution of the program in the try block, the exception will be captured and the corresponding except block will be found. To handle the exception, there is a question here, how does it find the corresponding except block?
We know that a try block can also correspond to multiple except blocks, and an except block can handle multiple exceptions at the same time. If we want to use an except block to handle all exceptions, we can write like this:
try: #...except Exception: #...
In this case, for the try block Any exception that may occur, the Python interpreter will be handed over to the only except block for processing, because its parameter is Exception, which means that any type of exception can be received.
Note that for except which can receive any exception, it can be followed by Exception or without any parameters, but the meaning is the same.
Here we will introduce Exception in detail. You should know that in order to represent various exceptions that may occur in the program, Python provides a large number of exception classes, and there are strict inheritance relationships between these exception classes. Figure 1 shows the inheritance relationship between Python's common exception classes.
As can be seen from Figure 1, BaseException is the base class of all exception classes in Python, but for us, the most important one is the Exception class, because in the program Various exceptions that may occur are inherited from Exception.
Therefore, if the user wants to implement a custom exception, he should not inherit BaseException but should inherit the Exception class. For information on how to customize an exception class, please read the section "Python Custom Exception Classes".
When the try block captures the exception object, the Python interpreter will compare the exception type with the exception class specified by each except block in turn. If the captured exception class is different from the exception class after an except block, If the exception class is the same, or is a subclass of the exception class, then the Python interpreter will call this except block to handle the exception; otherwise, the Python interpreter will continue to compare until it is compared with the last except. If the comparison is not successful, , it proves that the exception cannot be handled.
Figure 2 demonstrates the entire process from catching the exception to handling the exception when an exception occurs in the program in the try block.
Let’s look at a few simple exception capture examples:
try: a = int(input("输入 a:")) b = int(input("输入 b:")) print( a/b ) except ValueError: print("数值错误:程序只能接收整数参数") except ArithmeticError: print("算术错误") except Exception: print("未知异常")
In this program, depending on the user’s input of a and b values, it may cause ValueError, ArithmeticError exception:
If the a or b entered by the user are other characters instead of numbers, a ValueError exception will occur. The try block will capture this type of exception, and the Python interpreter will The first except block will be called to handle the exception;
If a and b entered by the user are numbers, but the value of b is 0, since the divisor cannot be 0 during division operation, Therefore, an ArithmeticError exception will occur, and the try block will catch the exception. At the same time, the Python interpreter will call the second except block to handle the exception;
Of course, during the running of the program, there may also be other reasons. If any exception occurs, the try block can catch it, and Python will call the last except block to handle it.
When a try block is equipped with multiple except blocks, these except blocks should follow an ordering rule that can handle all exception except blocks (the parameter is Exception, or whatever (neither written) should be placed after all except blocks, and all except blocks of parent class exceptions should be placed after the except blocks of subclass exceptions.
Recommended tutorial: "Python Tutorial"
The above is the detailed content of Detailed explanation of Python exception handling mechanism. For more information, please follow other related articles on the PHP Chinese website!