Originally, please see:
BEWARE The Dangers of Generic Exceptions
Capture and throwing a general anomalies will soon fall into troubles in unknowing.
summary
Java provides a wealth of exception handling framework, but although it is rich and easy to use, many programmers have easily ignored it. This article explores the risk of throwing, capturing and ignoring ordinary abnormalities, and presents how to handle complex abnormalities in the face of a comprehensive large software project. (October 3, 2003 by Paul Philion)
In a software project in the most recent work, I found code for processing the way the resource cleaning work. Because it has many different call forms, it will potentially throw 6 different exceptions. (Write this code) The original programmer tried to simplify the code (or want to save input), declare that this method will throw an Exception exception, not the six different exceptions. This allows the calling code to be encapsulated in the TRY / CATCH block captured Exception. He decided to do that because these codes were the purpose of resource cleaning, and the situation failed was not important, so the catch block is empty and the system is closed regardless of the system.
Obviously, this is not the best programming practice. In addition to a logical problem in the third line of the original code, there is no obvious mistake.
Program List 1: Original cleaning code
private void cleanupConnections () throws ExceptionOne, ExceptionTwo {for (int i = 0; i In some segment of other places, array consNections initializes when the first connection is established. But if there is no established, then Connections will be empty. So in some case, statement connections [i] .Release () call results will trigger an NullPointerexception exception. This is a fairly easy to repair problems. Just add a check statement "connections! = Null". However, there is an exception that will never be reported. It will be thrown by CleanUpConnections (), and then throw it by cleanurerything () and finally captured in Done (). The exception handling of the DONE () method does not do anything, and even there is no log record. Because cleanurerything () is called only by done (), this exception will never be seen. So the code will never be fixed. In this case, suppose now CleanupConnections () will not fail, then the cleanupfiles () and removelisteners () methods will never be called (resources will never release), and Domorestuff () will never be called, this is in DONE The last process in () will never be completed. In order to make things worse, done () is not called when the system is turned off, and the resource leak occurs in each transaction. The problem is obvious, mainly: Errors are not reported and resource leakage. However, the code itself does not seem to have a mistake, and this problem has increased the difficulty of tracking from the code writing method. However, by applying some simple guidelines, this problem will be found and repaired: Don't ignore an exception Don't capture exceptions Don't throw generic Exceptions Don't ignore the most obvious problem in code list 1 is completely ignored in an error in the program. A non-expected abnormality (abnormal born is not expected) is thrown, and the code has not intended to handle the exception. This exception is not even reported because the code assumes that the expected exception is not important. In most cases, an exception should be at least recorded. There are several log packs (see "Logging Exceptions") to record system errors and have no significant impact on the system. Most log systems also allow stack records to be printed, which provides a valuable information that occurs where and is exception. Finally, because the log is written to the file, an exception record can be viewed and analyzed. You can look at "There is an example of a record stack tracking in" Logging Excetions ". In some special circumstances, the log exception is not critical, such as the resource cleaning in the finally clause is one of them. Finally Exceptions (Exceptions) In the following code Listing 2, you need to read data from one file. Whether you need to turn off whether an exception file occurs when you read the data, the close () method is placed in the finally clause. But if and then closing the file when an error occurs, it is powerless code in Listing 2:. public void loadFile (String fileName) throws IOException {InputStream in = null; try {in = new FileInputStream (fileName); readSomeData (in);} finally { IF (in! = null) {Try {in.close ();} catch (ioexception ooe) {// ignored}}}} Attention Method's call still throws an ioException. Another point, even if it is ignored in Close (), the code declaration (in the comment statement line) makes it clear to anyone who uses it. You can apply this To clean any I / O stream, close Socket and JDBC connections. When ignoring an exception, the key is to ensure that only one method to ignore (other methods may be called outside) And there is a designated exception being captured. This special situation is significantly different from capturing a normal Exception. In all other cases, an exception should be (at least) to be recorded in the log, and its content with stack tracking record Suitable.