The benefits of python exception handling mechanism _ python uses exception handling mechanism correctly

1. Don’t overuse exceptions

It is undeniable that Python's exception mechanism is indeed convenient, but abuse of the exception mechanism will also bring some negative effects. The overuse exception is mainly manifested in two aspects:

Confuse exceptions with common errors, no longer write any error handling code, but simply throw exceptions instead of all error handling.

Use exception handling instead of flow control.

After becoming familiar with exception usage, programmers may no longer want to write cumbersome error-handling code and simply throw exceptions. In fact, this is wrong. For fully known errors and common errors, code to deal with such errors should be written to increase the robustness of the program. Use exceptions only for external, undetermined and predictable runtime errors.

2. Don't use too large try blocks

Many readers of the novice exception mechanism like to put a lot of code in the try block, which looks very "simple", but this "simple" is just an illusion, it just looks simpler when writing programs. However, because the code in the try block is too large and the business is too complex, the possibility of exceptions in the try block will be greatly increased, which will greatly increase the difficulty of analyzing the cause of the exception.

And when the block is too large, it is inevitable to follow a large number of except blocks after the try block to provide different processing logic for different exceptions. Following a large number of except blocks in the same try block requires analyzing the logical relationship between them, which increases the programming complexity.

The correct approach is to divide the large try block into multiple program sections that may cause exceptions, and put them in separate try blocks to catch and handle exceptions separately.

3. Don't ignore caught exceptions

Don't ignore exceptions! Now that the exception has been caught, the except block should do something useful and handle and fix the exception. An entirely empty except block, or simply printing a simple exception message, is inappropriate! ~

If the except block is empty, it pretends not to know or even hides it from the sky. This is the most terrible thing. When the program goes wrong, no one can see any exceptions, but the whole application may be completely broken. Printing the exception propagation information only in the except block is slightly better, but only a few more lines of exception information than blank. Appropriate action on exceptions is generally recommended, such as:

Handle exceptions. Make appropriate repairs to the exception, and then bypass the place where the exception occurred and continue to run; or use other data to calculate instead of the expected return value of the method; or prompt the user to re-operate... In short, the program should try to repair the exception so that the program can can resume operation.

Re-raises a new exception. Do what can be done in the current operating environment as much as possible, then translate the exception, wrap the exception into the exception of the current layer, and re-transmit it to the upper-layer caller.

Handle exceptions at the appropriate layer. If the current layer does not know how to handle the exception, do not use the except statement in the current layer to catch the exception, and let the upper-layer caller be responsible for handling the exception.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326763902&siteId=291194637