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?
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:
Catch exceptions that are known to be handled, and error record other exceptions for programmers to handle manually
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