JDK-8264696 : Multi-catch clause causes compiler exception because it uses the package-private supertype
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8-pool,11,14,15,16,17
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2021-04-03
  • Updated: 2022-07-08
  • Resolved: 2021-04-08
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.
JDK 17
17 b18Fixed
Related Reports
Relates :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
javac full version "16+36-2231"

A DESCRIPTION OF THE PROBLEM :
The usage of a multi-catch clause may cause a compiler exception, whereas the equivalent code with separate catch clauses doesn't. Given 2 public exception classes which extend a package-private exception class in pkg2, when I have a multi-catch clause for both public exception classes in pkg1 and attempt to access a method such as `getMessage` inside it, then the compiler uses their package-private parent and gives a compiler error saying `getMessage` is in an inaccessible class.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
* create the 4 following files with the code as below:
src\pkg1\Test.java
src\pkg2\Child1Exception.java
src\pkg2\Child2Exception.java
src\pkg2\ParentException.java
* compile the files:
javac -d out --source-path src src\pkg1\Test.java src\pkg2\Child1Exception.java src\pkg2\Child2Exception.java src\pkg2\ParentException.java

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compilation succeeds
ACTUAL -
src\pkg1\Test.java:22: error: Throwable.getMessage() is defined in an inaccessible class or interface
            e.getMessage();
             ^
1 error

---------- BEGIN SOURCE ----------
package pkg1;

import pkg2.Child1Exception;
import pkg2.Child2Exception;

class Test {

    void success() {
        try {
            foo();
        } catch (Child1Exception e) {
            e.getMessage();
        } catch (Child2Exception e) {
            e.getMessage();
        }
    }

    void fail() {
        try {
            foo();
        } catch (Child1Exception | Child2Exception e) {
            e.getMessage();
        }
    }

    void foo() throws Child1Exception, Child2Exception {
    }
}
====================================
package pkg2;

public class Child1Exception extends ParentException {
}
====================================
package pkg2;

public class Child2Exception extends ParentException {
}
====================================
package pkg2;

class ParentException extends Exception {
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Increase the accessibility of the parent class or use uni-catch clauses


Comments
Reply received from the submitter ============================== I’ve verified and the issue is indeed fixed, thanks.
11-05-2021

Requested the submitter to see if the bug is fixed in the latest build at https://jdk.java.net/17/ b21.
11-05-2021

Changeset: 57f1e7d9 Author: Guoxiong Li <lgxbslgx@gmail.com> Committer: Vicente Romero <vromero@openjdk.org> Date: 2021-04-08 21:34:51 +0000 URL: https://git.openjdk.java.net/jdk/commit/57f1e7d9
08-04-2021

Using a package-private parent gives a compiler error saying `getMessage` is in an inaccessible class when using multi catch statements. See attachment for the source code. The child exception classes are public. src\pkg1\Test.java:22: error: getMessage() in Throwable is defined in an inaccessible class or interface e.getMessage();
05-04-2021