...
This compliant solution specifies both an interface for reporting exceptions which exports the report()
method and also a default exception reporter class that the library can use. The exception reporter can be overridden by subclasses.
The setExceptionReporter()
method prevents hostile code from maliciously installing a more verbose reporter that leaks sensitive information or that directs exception reports to an inappropriate location, such as the attacker's computer, by limiting attempts to change the exception reporter to callers that have the custom permission ExceptionReporterPermission
with target exc.reporter
. (See SEC10-J. Define custom security permissions for fine grained security for more information regarding defining custom permissions.) Note that it is inappropriate to use a default permission such as java.util.logging.LoggingPermission
here because the logging permission's purpose is to control execution of specific logging methods (such as Logger.setLevel
) rather than to control setting the exception reporter itself.
The library may subsequently use the exception reporter in catch
clauses:
...
ERR00-EX1: When recovery from an exceptional condition is impossible at a particular abstraction level, code at that level should avoid handling must not handle that exceptional condition. In such cases, an appropriate exception must be thrown so that higher level code can catch the exceptional condition and can attempt recovery. The most common implementation for this case is to omit a catch
block and allow the exception to propagate normally:
...
Some APIs may limit the permissible exceptions thrown by particular methods. In such cases, it may be necessary to catch an exception and either wrap it in a permitted exception or translate it to one of the permitted exceptions.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.
Code Block | ||
---|---|---|
| ||
try { // Requested file does not exist // User is unable to supply the file name } catch (FileNotFoundException e) { throw new IOException(e); } new IOException(e); } |
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.
Wiki Markup |
---|
*ERR00-EX2:* An {{InterruptedException}} may be caught and suppressed when extending {{Thread}}. \[[Goetz 2006|AA. Bibliography#Goetz 06]\]. A interruption request may also be suppressed by code that implements a thread's interruption policy \[[Goetz 2006|AA. Bibliography#Goetz 06], pg. 143\]. |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e1262ed80a774106-e1c173c8-408d40ad-8e4d8c7a-bac3b746082c6bd00175e4cd"><ac:plain-text-body><![CDATA[ | [[MITRE 2009 | AA. Bibliography#MITRE 09]] | [CWE-390 | http://cwe.mitre.org/data/definitions/390.html] "Detection of Error Condition Without Action" | ]]></ac:plain-text-body></ac:structured-macro> |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="da472bea2d54310e-e023328a-4adb4ec5-818899aa-e8382a2f0b517be401eb5952"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 65: "Don't ignore exceptions" and 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="8992c0463161c91c-bc9ab809-447c4daa-b29eaad6-7ddae7f3393c23274f41002e"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#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="644ef8965cbc6b92-0b01bd80-4e43413c-be8796e6-55acfeb19454a48e6e9528d9"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [Chapter 11, Exceptions | http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html] | ]]></ac:plain-text-body></ac:structured-macro> |
...