Exception Handling experience

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.