JDK-8164613 : Compiler error with declared generic Exception type
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8,9
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2016-08-18
  • Updated: 2016-08-24
  • Resolved: 2016-08-24
Related Reports
Duplicate :  
Description
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.


Comments
Please use LambdaExceptionDeductionFuncTest file for verification. Please ignore Main.java file as it got attached accidently and not getting deleted from the list
23-08-2016

Issue observed when a derived interface changes the declared exception type from a fixed one to be a generic, compilation fails with an error. 8uxx - Fail (Including 8u101/102) 9 ea b- 131 - Fail == -sh-4.1$ /opt/java/jdk1.8.0_102/bin/javac LambdaExceptionDeduction2FuncTest.java LambdaExceptionDeduction2FuncTest.java:6: error: class LambdaExceptionDeductionFuncTest is public, should be declared in a file named LambdaExceptionDeductionFuncTest.java public class LambdaExceptionDeductionFuncTest ^ LambdaExceptionDeduction2FuncTest.java:21: error: get() in <anonymous LambdaExceptionDeductionFuncTest$> cannot implement get() in IThrowingGetter final IThrowingGetter <IOException> aGetter = () -> Character.toString ((char) aIS.read ()); ^ overridden method does not throw Exception
23-08-2016