If a finally clause is specified, irrespective of whether the try or catch block executes to completion or not, the finally block is executed. Conseuqnetly statements that abruptly exit from the finally block may cause related catch blocks to not be executed. Thus, keywords like return, break, continue and throw should never be used within a finally block.
Noncompliant Code Example
...
This compliant solution removes the return statement from the finally block. Likewise, keywords like break, continue and throw should never be used within a finally block.
| Code Block | ||
|---|---|---|
| ||
class SomeException extends Exception {
public SomeException(String s) {
super(s);
}
}
class TryFinally {
private static void throwException() throws SomeException {
throw new NullPointerException();
}
static private void doLogic() {
try {
throwException();
} catch (SomeException se) { System.out.println("Exception thrown"); }
finally {
System.out.println("Uncaught Exception");
}
return true;
}
public static void main(String[] args) {
doLogic();
}
}
|
...