Home  >  Article  >  Backend Development  >  According to the specification, should all exceptions that may be thrown be caught?

According to the specification, should all exceptions that may be thrown be caught?

WBOY
WBOYOriginal
2016-10-10 11:56:01967browse

Sometimes I know clearly that doing this will never cause an exception, but do I still have to catch it?

Sometimes, the error that is triggered is fatal and is not caused by user operation. In this way, even if it is caught, it is still dead. In this way, what is the meaning of catching this exception?

However, I always see a lot of people saying that they must not just throw and ignore it, not catch.

So what should we do?

Reply content:

Sometimes I know clearly that doing this will never cause an exception, but do I still have to catch it?

Sometimes, the error that is triggered is fatal and is not caused by user operation. In this way, even if it is caught, it is still dead. In this way, what is the meaning of catching this exception?

However, I always see a lot of people saying that they must not just throw and ignore it, not catch.

So what should we do?

Personally, I feel that there is a certain degree of error handling that needs to be grasped, but it is very difficult to grasp this degree.

My understanding is:

  • First of all, basic and necessary exceptions must be made.

  • Secondly, there is no need to catch all exceptions as much as possible, otherwise

    • Long error handling code

    • Execution efficiency issues

    • Some exceptions have been caught, but there is no better way to deal with them; for example, if it is a bug in the code, we should expose the bug in the code as much as possible and solve it as soon as possible.

  • Finally, to summarize, exception handling requires a certain degree of control. I think this degree is:

    • Do basic and necessary exception handling

    • Don’t deliberately do all exception handling in pursuit of perfection. Some problems should be exposed and solved as soon as possible

My experience:

  1. Catch exceptions that are known to be handled, and error record other exceptions for programmers to handle manually

  2. Exception handling is hierarchical. When implementing your own module, include the exception handling of dependent third-party modules in your own module

10 Best Practices for Java Exception Handling

  • However, there are always people who like to sum up a set by themselves like tossing a wheel. After climbing out of the pit, they find that the wheels made by others are so good (laughing).

The calculation is that catching does not necessarily mean dying. The purpose of catching exceptions is mainly to handle exceptions rather than die. For example, when sending an email fails due to network reasons, throwing an exception at this time will fail. The character is placed in the queue to be processed again.
Exceptions are for the program to be more robust

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn