Versions Compared

Key

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

...

Code Block
bgColor#FFCCCC
class TryFinally {
  private static boolean doLogic() {
    try {
      throw new IllegalStateException();
    } finally {
      System.out.println("Uncaughtlogic Exceptiondone");
      return true;
    }
  }

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

The IllegalStateException is suppressed by the abrupt termination of the finally block caused by the return statement.

...

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

ERRO4-EX0: Control flow statements whose destination is within the finally block are perfectly acceptable.

For instance, the following code does not violate this rule, because the break statement exits the while loop, but not the finally block.

Code Block
bgColor#ccccff

class TryFinally public static void main(String[] args) {
    doLogic();{
  private static boolean doLogic() {
    try {
      throw new IllegalStateException();
    } finally {
      int c;
      try {
        while ((c = input.read()) != -1) {
          if (c > 128) {
            break;
          }
        }
      } catch (IOException x) {
        // forward to handler
      }
      System.out.println("logic done");
    }
    // Any return statements must go here; applicable only when exception is thrown conditionally
  }
}

Risk Assessment

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

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a30196cd4d2a0d6b-006aced9-4cd44581-a504a6c0-393a28d1c761b00394c0ccdc"><ac:plain-text-body><![CDATA[

[[MITRE 2009

AA. Bibliography#MITRE 09]]

[CWE-705

http://cwe.mitre.org/data/definitions/705.html] "Incorrect Control Flow Scoping" and [CWE-584

http://cwe.mitre.org/data/definitions/584.html] "Return Inside Finally Block"

]]></ac:plain-text-body></ac:structured-macro>

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3627be659b56aeb4-9444ce97-45ac4c59-bfc7b521-48e927a82aae55d14babf9ac"><ac:plain-text-body><![CDATA[

[[Bloch 2005

AA. Bibliography#Bloch 05]]

Puzzle 36: Indecision

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b6c5f4ec71ce50b5-294634b4-45954d55-b5efa882-5d78dc73679f20b7643deef6"><ac:plain-text-body><![CDATA[

[[Chess 2007

AA. Bibliography#Chess 07]]

8.2 Managing Exceptions, "The Vanishing Exception"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="866da98bb0434b8b-41c907b8-4a28462b-b8838ed4-c2120a8477e5058a2af402c6"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[§14.20.2, Execution of try-catch-finally

http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.20.2]

]]></ac:plain-text-body></ac:structured-macro>

...