1. Exception Handling versus returned errorsCodeOne of the biggest advantages is that exceptions can be automatically transmitted, which makes it more difficult to ignore exceptions during programming.
2. Usually, exceptions are captured only at the top (usually the UI Layer. To catch exceptions at other layers, unless it is one of the following:
(1) can handle this exception, or
(2) can ignore this exception, or
(3) A new exception is thrown after the exception needs to be converted to another specific exception.
3. You can
(1) Ignore irrelevant exceptions.
(2) convert an exception to an error message and present it to the user.
(3) in case of a major exception, consider terminating the application.Program.
4. The top layer (generally the UI Layer) should not throw new exceptions; the top layer needs to capture all exceptions; otherwise, exceptions will directly lead to program termination, which will be a very bad user experience.
5. You can throw a custom exception in the non-top layer. If a custom exception occurs, make sure that the exception is serializable and that the exception is implemented by the three constructors.
6. It takes a lot of CPU time to throw and intercept exceptions. Please be careful when using exceptions.
7. Where exceptions may be thrown, if necessary, make sure to use finally for resource cleanup, regardless of whether exceptions are caught here.
8. The code in a Catch Block should at least partially handle the caught exception. Otherwise, do not use catch blocks.
9. Throw an exception from the constructor.
The constructor does not return values. Therefore, there is no simple method for the constructor to send a signal of constructor failure. In this case, an exception can be thrown. For example, if the constructor parameter does not match the specified condition, an exception is thrown.
10. With the above premise, logs can be recorded on non-top layers using intercept exceptions. In this way, we can understand the running status of the system through logging. I can't remember where I saw such a sentence: In software implementation, exceptions and logs are important quality assurance measures, and exceptions and logs always occur at the same time.
Exception is an important/main component of log records.