Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1266866

break statement in finally block

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 1.1
    • 1.0.2
    • tools
    • 1.1
    • sparc
    • generic
    • Not verified



      Name: ###@###.### Date: 09/06/96


      Java language specification [14.18.2 Execution of try-catch-finally] says:

      "
      If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
         . . .
      If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
         . . .
      If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).
      "

      The following example represents situation when try block completes abruptly as arithmetic exception is thrown, this exception is not catched by any catch clause, finally block completes abruptly as break statement with label is used, and the try statement is break target.

      > javac -d . excp01903.java
      java.lang.NullPointerException
      at sun.tools.tree.BreakStatement.code(BreakStatement.java:72)
      at sun.tools.tree.CompoundStatement.code(CompoundStatement.java:137)
      at sun.tools.tree.FinallyStatement.code(FinallyStatement.java:143)
      at sun.tools.tree.CompoundStatement.code(CompoundStatement.java:137)
      at sun.tools.javac.SourceField.code(SourceField.java:406)
      at sun.tools.javac.SourceField.code(SourceField.java:368)
      at sun.tools.javac.SourceClass.compileClass(SourceClass.java:564)
      at sun.tools.javac.SourceClass.compile(SourceClass.java:527)
      at sun.tools.javac.Main.compile(Main.java:193)
      at sun.tools.javac.Main.main(Main.java:289)
      error: An exception has occurred in the compiler; please file a bug report (###@###.###).
      1 error
      >

      --------------------------------excp01903.java--------------------------------
      //File: @(#)excp01903.java 1.3 96/09/06
      //Copyright 09/06/96 Sun Microsystems, Inc. All Rights Reserved
       
      package javasoft.sqe.tests.lang.excp019.excp01903;

      import java.io.PrintStream;

      public class excp01903 {
        public static void main(String argv[]) {
           System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
        }
        public static int run(String argv[],PrintStream out) {
           int a1 = 0;
           int a2 = 0;
           int n1 = 0;
           int n2 = 0;
           int result = 2;
        M: try {
              try {
                 a1 = 6;
                 a2 = 6/(a1-6);
              }
              catch (ArrayStoreException e) {
                 n1 = 1;
              }
              finally {
                 a1 = 1;
              }
           }
           catch (IndexOutOfBoundsException e) {
              n2 = 2;
           }
           finally {
              a2 = 2;
              break M;
           }
           if ( n1 == 0 && n2 == 0 && a1 == 1 && a2 == 2 )
              result = 0;
           return result;
        }
      }
      ----------------------------------------------------------------

      If labeled try statement put into labeled block this modified test can be compiled and executed successfully:

      --------------modified excp01903.java--------------------------------
      //File: @(#)excp01903.java 1.3 96/09/06
      //Copyright 09/06/96 Sun Microsystems, Inc. All Rights Reserved
       
      package javasoft.sqe.tests.lang.excp019.excp01903;

      import java.io.PrintStream;

      public class excp01903 {
        public static void main(String argv[]) {
           System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
        }
        public static int run(String argv[],PrintStream out) {
           int a1 = 0;
           int a2 = 0;
           int n1 = 0;
           int n2 = 0;
           int result = 2;
        M: {
              try {
                 try {
                    a1 = 6;
                    a2 = 6/(a1-6);
                 }
                 catch (ArrayStoreException e) {
                    n1 = 1;
                 }
                 finally {
                    a1 = 1;
                 }
              }
              catch (IndexOutOfBoundsException e) {
                 n2 = 2;
              }
              finally {
                 a2 = 2;
                 break M;
              }
           }
           if ( n1 == 0 && n2 == 0 && a1 == 1 && a2 == 2 )
              result = 0;
           return result;
        }
      }
      ----------------------------------------------------------------
      ======================================================================

            fyellinsunw Frank Yellin (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: