Programmers often suppress checked exceptions. That is, they often catch exceptions with a catch block, with the catch body doing nothing or something trivial, such as printing the stack trace. Often the catch body will have a comment stating that the exception is explicitly being ignored.
catch-and-ignore checked exceptions. Exceptions must be handled appropriately. There are few valid reasons for ignoring suppressing exceptions; the least uncommon are cases where the client cannot be expected to recover from the underlying problem. Even in In these cases, it is usually good practice to allow the exception to propagate outwards rather than to catch and ignore suppress the exception.
Catching and suppressing exceptions is considered bad practice for several reasons. Exceptions disrupt the expected control flow of the application. For example, statements in the try
block that follow the statement that caused the exception are skipped.
...
Printing the exception's stack trace can be useful for debugging purposes but results in program execution that is equivalent to ignoring suppressing the exception.
Code Block | ||
---|---|---|
| ||
try { //... } catch(IOException ioe) { ioe.printStacktrace(); } |
...
The run()
method of a Runnable
object cannot throw a checked exception. Consequently, this noncompliant code example catches and ignores suppresss java.lang.InterruptedException
.
...
EXC00-EX1: It is reasonable to ignore suppress handling an exception that occurs within a catch
or finally
block, such as when closing a FileInputStream
object.
...
Wiki Markup |
---|
*EXC00-EX3:* "The only situation in which it is acceptable to swallow an interrupt is when you are extending Thread and therefore control all the code higher up on the call stack" \[[Goetz 2006|AA. Bibliography#Goetz 06]\]. In such cases {{InterruptedException}} may be caught and ignoredsuppressd. A interruption request may also be swallowed by code that implements a thread's interruption policy \[[Goetz 2006, pg 143|AA. Bibliography#Goetz 06]\]. |
...