'overridden method does not throw Exception' when use lambda with override exception thrown

426 Views Asked by At

To prevent to be closed as duplicate, this question is totally different with this one.

Consider I have the following interface

@FunctionalInterface
interface FuncE0<R, E extends Exception> {
  R call() throws E;
}

It works fine with lambda

FuncE0<Integer, IOException> get() {
  return () -> 1;
}

But if I let the interface extends from Callable, it breaks.

@FunctionalInterface
interface FuncE0<R, E extends Exception> extends Callable<R> {
  @Override
  R call() throws E;
}

With the same usage. Complier gives me following error

JustTest.java:8: error: call() in <anonymous JustTest$> cannot implement call() in FuncE0
    return () -> 1;
           ^   overridden method does not throw Exception
  • If you remove the override method R call() throws E in FuncE0, it works.
  • If you use anonymous class, it works.
  • If you use eclipse, ECJ works.

What happened when I override the exception thrown? Is this a javac bug?

I'm using jdk_1.8_112

Minimize code to reproduce

import java.io.IOException;
import java.util.concurrent.Callable;

public class JustTest {
  public static FuncE0<Integer, IOException> get() {
    return () -> 1;
  }

  @FunctionalInterface
  public interface FuncE0<R, E extends Exception> extends Callable<R> {
    @Override
    R call() throws E;
  }
}
1

There are 1 best solutions below

0
On BEST ANSWER

This is a jdk bug which has been fixed 1.8.0_171(may not the lowest fix version).