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

exception from a throw statement in static initializer is not caught

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.1
    • CPU:
      x86, sparc
    • OS:
      solaris_2.4, windows_95
    • Verification:
      Not verified

      Description



      Name: vsC45997 Date: 11/10/96


      The section "14.16 The throw Statement" of The Java Language
      Specification contains the following:

      "If a throw statement is contained in a static initializer (8.5), then
      a compile-time check ensures that either its value is always an
      unchecked exception or its value is always caught by some try statement
      that contains it. If, despite this check, the value is not caught by
      some try statement that contains the throw statement, then the value
      is rethrown if it is an instance of class Error or one of its
      subclasses; otherwise, it is wrapped in an ExceptionInInitializerError
      object, which is then thrown (12.4.2). "

      The following examples show that exceptions thrown from a static
      initializer are not caught. Class "stmt10701a" throws the instance of
      "Error" class and class "stmt10702a" throws the instance of
      "RuntimeException".

      > javac -d . stmt10701.java
      > java -verify javasoft.sqe.tests.lang.stmt107.stmt10701.stmt10701
        before
      java.lang.Error
      at javasoft.sqe.tests.lang.stmt107.stmt10701.stmt10701a.<clinit>(stmt10701.java:30)
      at javasoft.sqe.tests.lang.stmt107.stmt10701.stmt10701.run(stmt10701.java:11)
      at javasoft.sqe.tests.lang.stmt107.stmt10701.stmt10701.main(stmt10701.java:5)
        after
      failed


      > javac -d . stmt10702.java
      > java -verify javasoft.sqe.tests.lang.stmt107.stmt10702.stmt10702
        before
      java.lang.RuntimeException
      at javasoft.sqe.tests.lang.stmt107.stmt10702.stmt10702a.<clinit>(stmt10702.java:30)
      at javasoft.sqe.tests.lang.stmt107.stmt10702.stmt10702.run(stmt10702.java:11)
      at javasoft.sqe.tests.lang.stmt107.stmt10702.stmt10702.main(stmt10702.java:5)
        after
      failed


      --------------------------stmt10701.java------------------------------
      package javasoft.sqe.tests.lang.stmt107.stmt10701;
      import java.io.PrintStream;
      public class stmt10701 {
        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 = 1;
           int aa = 1;
           try {
              out.println(" before");
              aa = stmt10701a.a2;
              out.println(" after");
           }
           catch (Error e) {
              out.println(e);
              a1 = 2;
           }
           catch (Throwable e) {
              out.println(e);
           }
           if ( a1!=2 ) {
              out.println("failed");
              return 2;
           }
           return 0;
        }
      }
      class stmt10701a {
         static Error a = new Error();
         static int a1;
         static int a2 = 1;
         static { a1 = 1;
                  if ( a1 == 1 )
                        throw a;
                  else
                     a2 = 2;
                }
      }
      -----------------------------------------------------------------
      --------------------------stmt10702.java-----------------------------
      package javasoft.sqe.tests.lang.stmt107.stmt10702;
      import java.io.PrintStream;
      public class stmt10702 {
        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 = 1;
           int aa = 1;
           try {
              out.println(" before");
              aa = stmt10702a.a2;
              out.println(" after");
           }
           catch (RuntimeException e) {
              out.println(e);
              a1 = 2;
           }
           catch (Throwable e) {
              out.println(e);
           }
           if ( a1!=2 ) {
              out.println("failed");
              return 2;
           }
           return 0;
        }
      }
      class stmt10702a {
         static RuntimeException a = new RuntimeException();
         static int a1;
         static int a2 = 1;
         static { a1 = 1;
                  if ( a1 == 1 )
                        throw a;
                  else
                     a2 = 2;
                }
      }
      -----------------------------------------------------------------

      ======================================================================

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: