Returning from a finally
block suppresses the propagation of any unhandled Throwable
which was thrown in the try
or catch
block.
The following code snippet illustrates this rule. The developer expects to get "ERROR" in the console whereas "OK" is displayed.
public static void main(String[] args) { try { doSomethingWhichThrowsException(); System.out.println("OK"); } catch (RuntimeException e) { System.out.println("ERROR"); } } public static void doSomethingWhichThrowsException() { try { throw new RuntimeException(); } finally { /* ... */ return; // Non-Compliant - prevents the RuntimeException from being propagated } }