JDK-8289975 : Multi-catch leads to IllegalAccessError when least-upper-bound is inaccessible
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 19,20
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2022-07-08
  • Updated: 2022-08-01
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Blocks :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
The following run-time exception is raised

    > Exception in thread "main" java.lang.IllegalAccessError:
    > failed to access class pkg.Exceptions$EBase from class ExceptionsTest
    > (pkg.Exceptions$EBase and ExceptionsTest are in unnamed module of loader 'app')
    >     at ExceptionsTest.main(ExceptionsTest.java:8)

when running "java ExceptionsTest" program with

    > package pkg;
    > public class Exceptions {
    >     private static class EBase extends Exception {
    >         public void m() {}
    >     }
    >     public static class E1 extends EBase {}
    >     public static class E2 extends EBase {}
    > }

and

    > import pkg.Exceptions;
    > public class ExceptionsTest {
    >     public static void main(String[] args) {
    >         try {
    >             m1();
    >         } catch (Exceptions.E1 | Exceptions.E2 e) {
    >             e.m();
    >         }
    >     }
    >     private static void m1() throws Exceptions.E1, Exceptions.E2 {
    >         throw new Exceptions.E1();
    >     }
    > }

Source(s):
- https://gist.github.com/DasBrain/61d2859a66183d29cb2eb9bf4cc1c81b
- https://twitter.com/lukaseder/status/1544713480024068098
Comments
A similar, but different situation arises when using the conditional operator: (true ? new Exceptions.E1() : new Exceptions.E2()).m(); This produces the following compiling error: ExceptionsTest.java:5: error: EBase.m() is defined in an inaccessible class or interface (test() ? new Exceptions.E1() : new Exceptions.E2()).m(); ^ 1 error error: compilation failed Changing the code to call `getMessage()` instead, it yields the following error message: ExceptionsTest.java:5: error: Throwable.getMessage() is defined in an inaccessible class or interface (test() ? new Exceptions.E1() : new Exceptions.E2()).getMessage(); ^ 1 error error: compilation failed Which (wrongly) indicates that `Throwable` is not accessible. Surprisingly, this snippet compiles fine: (true ? new StringBuffer() : new StringBuilder()).toString(); But throws an IllegalAccessException at runtime.
10-07-2022