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

JLS3 14.21 Spec for reachability doesn't consider unchecked exceptions

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic, x86, sparc
    • OS:
      generic, solaris_2.5, solaris_2.6, solaris_7, solaris_8, windows_95, windows_98

      Description

      Name: ngC57085 Date: 04/22/97



      Java compiler (JDK1.1.2) does not report about unreachable catch block
      while compile try statement.

      JLS (14.19) says:

      "
      A catch block C is reachable iff both of the following are true:

         Some expression or throw statement in the try block is reachable and can
         throw an exception whose type is assignable to the parameter of the catch
         clause C. (An expression is considered reachable iff the innermost statement
         containing it is reachable.)

         There is no earlier catch block A in the try statement such that the type
         of C's parameter is the same as or a subclass of the type of A's parameter.
      "

      A catch block is not reachable if there is no some expression or throw
      statement in the try block that can throw an exception.

      Test stmt15204, where try block is empty, is compiled successfully by JDK1.1.2 instead of error message.

      > javac112 stmt15104.java
      >

      ------------------------stmt15104--------------
      import java.io.PrintStream;

      public class stmt15104 {
        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 a2 = 1;
           int a3 = 1;
           int a4 = 1;
           a1 = 2;
           try {
           }
           catch (ArithmeticException e) {
              a2 = 2;
           }
           catch (Exception e) {
              a3 = 2;
           }
           a4 = 2;
           return 2/*STATUS_FAILED*/;
        }
      }
      --------------------------------------

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

      From duplicate bug 4313756:

      Compilation of the following examples successfuly complete, but error message expected.

      class X {
         void check_unreachable () {
            try {
            } catch(Exception e) {
               int i = 10; // error: statement not reached
            }
         }
      }

      class X {
         static void check_unreachable () {
            try {
               return;
            } catch(Error e) {} // error: statement not reached
         }
      }

      This behavior violates JLS 14.19

      konstantin.boudnik@eng 2000-02-17

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

      From duplicate bug 4087302:


      allan.jacobs@Eng 1997-10-20

      The following example is motivated by Modena test n1419206.

      // import java.io.IOException;
      class X {
        static void check_unexception () {
          try{ }
          catch( Exception e) { System.out.println(e); }
        }
        static void check_unreachable () {
          try{ }
          catch( Throwable e) { System.out.println(e); }
        }
        public static void main( String[] argv ) {
          X.check_unthrowable();
          X.check_unexception();
        }
      }

      Section 14.19:
      "A catch block C is reachable iff both of the following are true:
      * Some expression or throw statement in the try block is reachable and
        can throw an exception whose type is assignable to the parameter of the
        catch clause C. (An expression is considered reachable iff the
        innermost statement containing it is reachable.)
      * There is no earlier catch block A in the try statement such that the
        type of C's parameter is the same as or a subclass of the type of A's
        parameter.

      Are the println's reachable? Javac says so but the spec (at least by
      the most direct reading) says not. Which catch block arguments do make
      their catch block's unreachable when their corresponding try blocks are
      empty?

      =========================================================================
      Here's another test case from ###@###.###. I would expect this one to fail at compile-time also.
      brian.klock@eng 1998-07-23

      //-----------------------------Test1.java---------------------------
      public class Test1 {
          public static void main(String args[]) throws Exception
          {
              try { throw new Exception(); }
              catch(SubException e) { System.out.println("Caught 1"); }

              try { m(); }
              catch(SubException e) { System.out.println("Caught 2"); }
          }

          static void m() throws Exception { throw new SubException(); }
      }

      class SubException extends Exception { }
      ###@###.### 10/22/04 14:12 GMT

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              abuckley Alex Buckley
              Reporter:
              duke J. Duke (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: