How to handle exceptions better

Source: Internet
Author: User
Tags exception handling
In the code writing process, always feel that they are not so appropriate to handle the exception, on the Internet to find some articles to look at. The records are as follows. now that the exception is caught, it should be handled appropriately. Do not catch the exception and then discard it, ignore.
You can make the following selections:
1) Handling Exceptions. Take action on the exception, such as correcting the problem, reminding a person, or doing something else, to determine what action should be taken according to the specific situation. Again, the call Printstacktrace is not considered "handled the exception."
2) to throw the exception again. The code that handles the exception, after parsing the exception, thinks that it cannot handle it, and throwing the exception back is an option.
3 Converts the exception to another exception. In most cases, this is an exception that converts a low-level exception to an application-level exception whose meaning is more easily understood by the user.
4 do not catch exceptions.
Specify the specific exception type as much as possible in the catch statement, and use multiple catch if necessary. Do not attempt to handle any exceptions that may occur.
Handle possible exceptions, not all exceptions, and can be thrown for exceptions that you do not want to handle directly.
ensure that all resources are released correctly. Make full use of finally keywords.
provide appropriate error reason information in the exception handling module, and organize error messages to make them easy to understand and read.
try to reduce the size of the try block.
It's not easy to find when there's an exception.
consider the possible exceptions and the impact of these exceptions on the execution process.
If there is an exception in the middle of processing some of the data, there will be incomplete data. The ideal solution is to write some information to the output device, declaring the data incomplete, and another possible method is to buffer the data to be output, to prepare all the data and then output it once.
The purpose of the exception handling is to handle the various errors and let the program continue to execute. Now you can think about the use of throw, we need to handle the exception as much as possible in the layer, rather than simply throw, let the outer processing. If there is any problem with the layer, the exception is thrown to main, and the program exits. The normal practice is to process in the inner layer, until the main, still can not handle the exit program. Of course, to ensure that the business is normal.

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.