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

Compile error in multi type catch block with Java 11, runtime error with Java 17

XMLWordPrintable

    • x86_64
    • linux

      A DESCRIPTION OF THE PROBLEM :
      There are two source files:

      package p;
      public class Exceptions {
          private static class E extends Exception {
              public void m() { }
          }
          public static class E1 extends E {}
          public static class E2 extends E {}
      }

      package q;
      import p.Exceptions;
      import p.Exceptions.*;
      public class Test {
          public static void main(String[] args) {
              try {
                  if (false) { throw new E2(); }
                  throw new E1();
              } catch (E1 | E2 e) {
                  e.m(); // javac 11: ...defined in an inaccessible class...
              }
          }
      }

      Compiling with Java 11 results in an error:

      q/Test.java:12: error: E.m() is defined in an inaccessible class or interface
                  e.m(); // javac 11: ...defined in an inaccessible class...
                   ^
      1 error

      Compiling with Java 17 doesn't result in an error, but running the code results in a runtime exception:

      Exception in thread "main" java.lang.IllegalAccessError: failed to access class p.Exceptions$E from class q.Test (p.Exceptions$E and q.Test are in unnamed module of loader 'app')
              at q.Test.main(Test.java:12)

      The difference in behavior between Java 11 and Java 17 is probably introduced with: https://bugs.openjdk.org/browse/JDK-8264696

      The bug-fix was done for: https://youtrack.jetbrains.com/issue/IDEA-297529
      A similar bug was opened for ecj: https://github.com/eclipse-jdt/eclipse.jdt.core/issues/198

      See also the following comments:

      https://github.com/eclipse-jdt/eclipse.jdt.core/issues/198#issuecomment-1177897759
      https://youtrack.jetbrains.com/issue/IDEA-297529/Wrong-compilation-error-reported-when-multi-type-catch-block-references-visible-method-from-invisible-type#focus=Comments-27-6280209.0-0

      REGRESSION : Last worked in version 11

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the 2 sources from the description.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A compile error reported by the compiler.
      ACTUAL -
      No compile error is reported, there is a runtime exception when running the code.

      ---------- BEGIN SOURCE ----------
      package p;
      public class Exceptions {
          private static class E extends Exception {
              public void m() { }
          }
          public static class E1 extends E {}
          public static class E2 extends E {}
      }

      package q;
      import p.Exceptions;
      import p.Exceptions.*;
      public class Test {
          public static void main(String[] args) {
              try {
                  if (false) { throw new E2(); }
                  throw new E1();
              } catch (E1 | E2 e) {
                  e.m(); // javac 11: ...defined in an inaccessible class...
              }
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        1. Exceptions.java
          0.2 kB
        2. Test.java
          0.3 kB

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: