Rule 9:Don’t ignore exceptions
Bad Code:
try {
…
} catch (SomeException e) {
}
Note: An EMPTY CATCH BLOCK DEFEATS THE PURPOSE OF EXCEPTIONS,which is to foce you to handle Exceptional Conditions.
Ignoring an exception is ANALOGOUS to ignoering the fire alarm, and turining it off so no one gets to see if there is real fire.
There are some situations where it is appropriate to ignore the exception.
Example :
it might be appropriate when closing a FileInputStream.Because we havenot modified the file so no need to run recovery code . and since we have read the info from file needed so no need to aborrt the operariton.
It may be wise to log the exception, so
that you can investigate the matter if these exceptions happen often.
If you Ignore the exceptoion follow the following rule:
Write the cause of ignoring the exception and the variable in catch bloack SHOULD be named “ignored”
Example:
Future f = exec.submit(planarMap::chromaticNumber);
int numColors = 4; // Default; guaranteed sufficient for any map
try {
numColors = f.get(1L, TimeUnit.SECONDS);
} catch (TimeoutException | ExecutionException ignored) {
// Use default: minimal coloring is desirable, not required
}
TODO: search other senario where the exceptions can be ignored or logged,because there are limited cases.