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