...
Code Block | ||
---|---|---|
| ||
class MyExceptionReporter extends ExceptionReporter { private static final Logger logger = Logger.getLogger("com.organization.Log"); public static void report(Throwable t) { try { final Throwable filteredException = (t instanceof NonSensitiveException_1) ? t : filter(t); } finally { // Do any necessary user reporting // (show dialog box or send to console) if (filteredException instanceof NonSensitiveCommonException) { logger.log(Level.FINEST, "Loggable exception occurred", t); } } } public static Exception filter(Throwable t) { if (t instanceof SensitiveForLoggingException_1) { // Do not log sensitive information (whitelist) return SensitiveCommonException(); } // ... // Return for reporting to the user return new NonSensitiveCommonException(); } } |
The {{ Wiki Markup report()
}} method accepts a {{Throwable
}} instance and consequently handles all errors, checked exceptions, and unchecked exceptions. The filtering mechanism is based on a _whitelisting_ approach wherein only non-sensitive exceptions are propagated to the user. Exceptions that are forbidden to appear in a log file can be filtered in the same fashion (see rule [FIO13-J. Do not log sensitive information outside a trust boundary]. This approach provides the benefits of exception chaining by reporting exceptions tailored to the abstraction while also logging the low-level cause for future failure analysis \ [[Bloch 2008|AA. References#Bloch 08]\].].
Noncompliant Noncompliant Code Example
If a thread is interrupted while sleeping or waiting, it causes a java.lang.InterruptedException
to be thrown. However, the run()
method of interface Runnable
cannot throw a checked exception and must handle InterruptedException
. This noncompliant code example catches and suppresses InterruptedException
.
Code Block | ||
---|---|---|
| ||
class Foo implements Runnable { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // Ignore } } } |
This code prevents callers of the {{ Wiki Markup run()
}} method from determining that an interrupted exception occurred. Consequently, the caller methods such as {{Thread.start()
}} cannot act on the exception \ [[Goetz 2006|AA. References#Goetz 06]\]. Likewise, if this code were called in its own thread, it would prevent the calling thread from knowing that the thread was ]. Likewise, if this code were called in its own thread, it would prevent the calling thread from knowing that the thread was interrupted.
Compliant Solution
This compliant solution catches the InterruptedException
and restores the interrupted status by calling the interrupt()
method on the current thread.
Code Block | ||
---|---|---|
| ||
class Foo implements Runnable { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // Reset interrupted status } } } |
...
Consequently, calling methods (or code from a calling thread) can determine that an interrupt was issued \[ [Goetz 2006|AA. References#Goetz 06]\].].
Exceptions
ERR00-EX0: Exceptions that occur during the freeing of a resource may be suppressed in those cases where failure to free the resource cannot affect future program behavior. Examples of freeing resources include closing files, network sockets, shutting down threads, and so forth. Such resources are often freed in catch
or finally
blocks and never reused during subsequent execution. Consequently, the exception cannot influence future program behavior through any avenue other than resource exhaustion. When resource exhaustion is adequately handled, it is sufficient to sanitize and log the exception for future improvement; additional error handling is unnecessary in this case.
...
Alternatively, when higher level code is also unable to recover from a particular exception, the checked exception may be wrapped in an unchecked exception and rethrown.
*ERR00-EX2:* An {{ Wiki Markup InterruptedException
}} may be caught and suppressed when extending class {{Thread
}} \ [[Goetz 2006|AA. References#Goetz 06]\]. An interruption request may also be suppressed by code that implements a thread's interruption policy \ [[Goetz 2006|AA. References#Goetz 06], p. 143\].
Risk Assessment
Ignoring or suppressing exceptions can result in inconsistent program state.
...
Bibliography
Item 65. Don't ignore exceptions; Item 62. Document all exceptions thrown by each method | |||||
5.4, Blocking and interruptible methods | |||||
[JLS 2005] | |||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="05105a15-b248-45a3-aaf8-1be193fc333d"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. References#Bloch 08]] | Item 65. Don't ignore exceptions; Item 62. Document all exceptions thrown by each method | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bba30a5d-6529-4669-aa0d-02c16c79dbbd"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. References#Goetz 06]] | 5.4, Blocking and interruptible methods | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a4445cf0-cab0-46dc-9cb9-34a4b853555c"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. References#JLS 05]] | [Chapter 11, Exceptions | http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html] | ]]></ac:plain-text-body></ac:structured-macro> |
...
06. Exceptional Behavior (ERR) 06. Exceptional Behavior (ERR)