Python runtime exception management solution

1 Foreword

Python uses special objects called exceptions to manage errors that occur during program execution. Whenever an error occurs that overwhelms Python, it creates an exception object.

If you write code to handle the exception, the program will continue to run; if you do not handle the exception, the program will stop and display a traceback, which contains a report about the exception.

Exceptions are handled using try-except code blocks. The try-except code block allows Python to perform the specified operation and tells Python what to do when an exception occurs.

When a try-except code block is used, the program will continue to run even if an exception occurs.

2 Use try-except code blocks

We know that the divisor cannot be 0. When the programmer mistakenly sets the divisor to 0, a ZeroDivisionError will occur.

Implementation code:

try:print(6/0)
 except ZeroDivisionError:print("you can not divide by zero!")

operation result:

you can not divide by zero!

working principle:

Put the code that may have errors in the try code block. If it runs without errors, the except code block will be skipped; if an error is caused, Python will look for the except code block and execute the code in it.

In this way, when an error occurs, the user sees a friendly error message.

If there are other codes after try-except, the program will continue to run.

3 Use try-except-else code block

Implementation code:

print("Please input two numbers, and I will divide them.")print("Enter 'q' to quit.")while True:
 first_num =input("\nFirst number: ")if first_num =='q':break
 second_num =input("Second number: ")try:
 answer =int(first_num)/int(second_num)
 except ZeroDivisionError:print("you can not divide by zero!")else:print(answer)

operation result:

working principle:

Generally, the code that may occur exceptions is placed in the try code block.

Python tries to execute the code in the try code block. If an error occurs, the program code in the except is executed; if it runs normally, the code in the else code block is executed.

In this way, the program crash problem caused by possible errors is effectively avoided, and the program becomes more robust.

4 Use pass statement

Implementation code:

while True:
 first_num =input("\nFirst number: ")if first_num =='q':break
 second_num =input("Second number: ")try:
 answer =int(first_num)/int(second_num)
 except ZeroDivisionError:
 pass
 else:print(answer)

operation result:

working principle:

10 When the code execution error occurs, Python will execute the pass statement in the except code block. The pass statement neither shows traceback nor any output.

In addition, the pass statement can also act as a placeholder to remind the programmer that no statement has been defined at this place, and statements may be defined later.

5 summary

At this point, we have learned some ways to handle exceptions. We can decide whether to report errors to users and how to report them according to different needs, effectively avoiding program crashes caused by some exceptions.

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python runtime exception management solution
Python exception handling
Context management in Python
Python crawler gerapy crawler management
Solution to python alignment error
Python implements parking management system
Python implements car management system
Python common exception handling mechanism
Implementing student management system with python
Python realizes business card management system
Python3 realizes business card management system
Python business card management system development
Python devel installation failure problem solution
Python version business card management system
Implementation of python student management system