...
EXC14-EX0: A catch block may catch all exceptions to process them before re-throwing them. For example, filtering sensitive information from exceptions before the call stack leaves a trust boundary. Refer to guideline ERR06-J. Do not allow exceptions to expose sensitive information, as well as CWE 7 and CWE 388). In such cases, a catch block should catch Throwable
rather than Exception
or RuntimeException
.
This code sample catches all exceptions and wraps them in a custom DoSomethingException
before re-throwing them.
Code Block | ||
---|---|---|
| ||
class DoSomethingException extends Exception {
public DoSomethingException(Throwable cause) {
super( cause);
}
// other methods
};
private void doSomething() throws DoSomethingException {
try {
// code that might throw an Exception
} catch (Throwable t) {
throw new DoSomethingException(t);
}
}
|
Exception wrapping is a common technique to safely handle unknown exceptions. For another example, see rule ERR10-J. Do not let code throw undeclared checked exceptions.
Wiki Markup |
---|
*EXC14-EX1*: Task processing threads such as worker threads in a thread pool or the Swing event dispatch thread are permitted to catch {{RuntimeException}} when they call untrusted code through an abstraction such as {{Runnable}} \[[Goetz 2006 pg 161|AA. Bibliography#Goetz 06]\]. |
...