java--when to handle exception (which level), base wrapper basic class processing task as concise as possible, write log, check null and other run-time exceptions

Source: Internet
Author: User

1. Run-time exception and inspected exception

2. Prevent run-time exceptions in advance. The most common occurrence is the NPE, which is the basic responsibility of the programmer to check the NPE. Other, such as a run-time exception of 0 and other checks, requires the programmer to examine carefully, each function must be checked (unless it can be determined that there are no null pointers, etc.), even if the number of the IF () statement increases. Exceptions that cannot be pre-checked are implemented by catch numberformatexception, such as when parsing an externally transmitted string form number.

NULL:1) If it is externally obtained, it is necessary to check the null;2) if it is internal or logically guaranteed to be correct, you can not check null.

3. Handle the exception being inspected. such as fetching data from a database, HTTP links, etc. to obtain external data, must be checked exception handling, because this is the immediate normal operation may also be abnormal situation.

4. Basic classes, such as DB or connection classes, do not care about connection failures and are cared for by the upper class or connection pool.

5. Exception, handed to the caller to handle, if it is thought that the hierarchy should handle this exception, handle it yourself.

6. If exception is processed at its own level, the return value is best for the program to continue, but does not further process the numeric value of the data. For example: Return an empty list, then the caller receives the return value, does not break the program, and because the number of returned list is 0, does not run down one step. This implementation also requires the process as a whole to be a style, that is, each step of the need for the processing of empty objects. This way, when exception occurs, the caller treats the data equally, but the final result can reflect an error.

7. Log write to file, information to be as detailed as possible. Don't be too little, don't repeat.

8. Log monitoring program, timed (half a minute) to scan the log, starting from the last read where the error string was found, send an email alert.

9. Log monitoring, if the need for high reliability and real-time, you can consider the socket or Message Queuing and other inter-process communication methods.

10. Object-oriented is a kind of thought, learning a kind of thought is a long process.

java--when to handle exception (which level), base wrapper basic class processing task as concise as possible, write log, check null and other run-time exceptions

Related Article

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.