Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
class TryFinally {         
  private static boolean doLogic() {
    try {
      throw new IllegalStateException(); 
    } finally {
      System.out.println("Caught Exception");
    }
    // Any return statements must go here; applicable only when exception is thrown conditionally
  }

  public static void main(String[] args) {
    doLogic();	
  }
}

If this example had a return statement outside the finally block, the compiler would report an error because the return statement would be unreachable due to the unconditional throwing of IllegalStateException. If the exception were thrown conditionally, the return statement could be used without compilation error.

Risk Assessment

Exiting abruptly from a finally block masks any exceptions thrown inside the associated try and catch blocks.

...