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

14.21: Try statements, unreachable statements, and unchecked exceptions

XMLWordPrintable

    • Verified

      javac doesn't comply with the JLS on the handling of unreachable statements in the context of try blocks and unchecked exceptions. The following is specified to be an error:

      class Test {
          void f() {
              try {
              } catch (Error ex) {
      ; // unreachable
              }
          }
      }

      Without the semicolon, it is legal! (because there is no unreachable statement).

      These problems are probably spec issues. I would prefer to see the following changes to the spec:

      (1) Add "The Block of a catch block is reachable iff the catch block is reachable".

      That addresses the difference in behavior on introduction of the semicolon. Also, I'd like to see this

        # 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.


      changed to this

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

          * Either
      # 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.),
      or
      # the type of C's parameter is an unchecked exception type.
          * 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.

      That gives the compiler permission to consider any unchecked exception to be thrown in the try block, even if the try block is empty.

            abuckley Alex Buckley
            gafter Neal Gafter (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: