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

Multi-catch clause causes compiler exception because it uses the package-private supertype

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 17
    • 8-pool, 11, 14, 15, 16, 17
    • tools
    • b18
    • x86_64
    • windows_10
    • Verified

      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

            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: