...
This compliant solution catches any exception thrown, and wraps it inside a custom exception, thereby consequently limiting the exceptions that can be thrown.
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); } } |
This code is valid by ERR14-EX0 of guideline rule ERR14-J. Do not catch RuntimeException.
Exception wrapping is a common technique to safely handle unknown exceptions, for another example, see guideline rule ERR10-J. Do not let code throw undeclared checked exceptions.
...