Python exception handling (try…except)
In Python, if there are exceptions, we can write our own code to catch these exceptions and let the Python interpreter not terminate the program.
The statement that handles the exception is the try…except statement. We put the statement that may have an exception in the try clause, and put the processing statement after the exception into the except clause. Or take the addition of numbers and strings as an example, the code is as follows.
try: numberEight=8 stringEight="8" print(numberEight+stringEight) print ("No exception, everything went well") except: print("An exception occurred")
Because the statement that will cause an exception is placed in the try clause, when Python catches the exception, it will not execute the rest of the statement, but jump directly to the except clause to execute.
Run the code and get the result shown in Figure 1.
If the try clause executes without an exception, the code in the except clause will not be executed. Slightly modify the previous example, this time adding numberEight and numberEight.
try: numberEight=8 stringEight="8" print (numberEight + numberEight) print ("No exception, everything went well") except: print("An exception occurred")
Run the code and get the result shown in Figure 2.
You can see that the code successfully prints the added result this time, and continues to execute the code after the try clause. The code in the except clause is not executed.
We can not only judge whether there will be exceptions, but also deal with them according to different exceptions. Just write the exception type after except. Let’s divide a number by 0, then catch the ZeroDivisionError exception, and make a corresponding prompt, the code is as follows.
try: numberEight=8 print(numberEight/0) print ("No exception, everything went well") except ZeroDivisionError: print("This is a division by zero error")
Run the code and get the result as shown in Figure 3
As you can see, the program successfully caught the ZeroDivisionError exception and dealt with it accordingly.
In Python, a try clause can also correspond to multiple except clauses, each of which is used to handle different exceptions.
Let’s see an example:
- First, we use a while loop, the condition is True, which means that the loop will continue until a certain condition is met, and then the loop can be jumped out;
- Then ask the user to enter a non-zero number, if the user enters as required, it will print “no exception occurred” on the screen, and end the while loop; if the user input is not a number, it will be printed on the screen. “Enter a number instead of a character, please try again”; if the user enters 0, it will print on the screen “input error, 0 cannot be used as a divisor, please try again”,
code show as below:
while True: try: firstNumber=int(input("Please enter a non-zero number:")) secondNumber=10/firstNumber print("No exception occurred") break except ZeroDivisionError: print ("Incorrect input, 0 cannot be used as a divisor, please try again") except ValueError: print("Incorrect input, enter a number instead of a character, please try again")
Running this code, when the character “a” is input, it will prompt an input error, and the character cannot be input; when the number 0 is input, it will also prompt an input error, and 0 cannot be input, because 0 cannot be used as a divisor; only when the user enters a non-zero value After the number, the program ends normally, as shown in Figure 4.