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

Compiler error with declared generic Exception type

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_101"
      Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Debian Linux - OS name: "linux", version: "3.13.0-40-generic", arch: "amd64", family: "unix"
      Windows 7 (6.1.7601)

      A DESCRIPTION OF THE PROBLEM :
      When a derived interface changes the declared exception type from a fixed one to be a generic, compilation fails with an error.

      The received error message:
      /home/travis/build/phax/as2-lib/as2-lib/src/main/java/com/helger/as2lib/LambdaExceptionDeductionFuncTest.java:[31,51] get() in <anonymous com.helger.as2lib.LambdaExceptionDeductionFuncTest$> cannot implement get() in com.helger.as2lib.LambdaExceptionDeductionFuncTest.IThrowingGetter overridden method does not throw java.lang.Exception

      The error can be reproduced with artificial class LambdaExceptionDeductionFuncTest provided in the source code section.

      The problem disappears, if I spare the IBaseGetter like this:

      import java.io.ByteArrayInputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.nio.charset.StandardCharsets;
      public class LambdaExceptionDeduction2FuncTest
      {
        public static interface IThrowingGetter <EX extends Exception>
        {
          String get () throws EX;
        }

        public static void main (final String [] args) throws IOException
        {
          final InputStream aIS = new ByteArrayInputStream ("abc".getBytes (StandardCharsets.UTF_8));
          final IThrowingGetter <IOException> aGetter = () -> Character.toString ((char) aIS.read ());
          aGetter.get ();
        }
      }


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to compile the class LambdaExceptionDeductionFuncTest from the Source Code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The file should compile fine.
      ACTUAL -
      The current result is

      /home/travis/build/phax/as2-lib/as2-lib/src/main/java/com/helger/as2lib/LambdaExceptionDeductionFuncTest.java:[31,51] get() in <anonymous com.helger.as2lib.LambdaExceptionDeductionFuncTest$> cannot implement get() in com.helger.as2lib.LambdaExceptionDeductionFuncTest.IThrowingGetter overridden method does not throw java.lang.Exception

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.ByteArrayInputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.nio.charset.StandardCharsets;

      public class LambdaExceptionDeductionFuncTest
      {
        public static interface IBaseGetter
        {
          String get () throws Exception;
        }

        public static interface IThrowingGetter <EX extends Exception> extends IBaseGetter
        {
          String get () throws EX;
        }

        public static void main (final String [] args) throws IOException
        {
          final InputStream aIS = new ByteArrayInputStream ("abc".getBytes (StandardCharsets.UTF_8));
          final IThrowingGetter <IOException> aGetter = () -> Character.toString ((char) aIS.read ());
          aGetter.get ();
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The workaround is to not use the base interface with the fixed defined getter class.

        1. LambdaExceptionDeductionFuncTest.java
          0.7 kB
          Fairoz Matte
        2. Main.java
          0.4 kB
          Fairoz Matte

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

              Created:
              Updated:
              Resolved: