'overridden method does not throw Exception' 使用 lambda 并抛出覆盖异常
'overridden method does not throw Exception' when use lambda with override exception thrown
为防止重复关闭,此题与this one完全不同。
假设我有以下界面
@FunctionalInterface
interface FuncE0<R, E extends Exception> {
R call() throws E;
}
它与 lambda 配合得很好
FuncE0<Integer, IOException> get() {
return () -> 1;
}
但是如果我让接口从 Callable
扩展,它就会中断。
@FunctionalInterface
interface FuncE0<R, E extends Exception> extends Callable<R> {
@Override
R call() throws E;
}
用法相同。编译器给我以下错误
JustTest.java:8: error: call() in <anonymous JustTest$> cannot implement call() in FuncE0
return () -> 1;
^ overridden method does not throw Exception
- 如果您删除
FuncE0
中的覆盖方法 R call() throws E
,它将起作用。
- 如果您使用匿名 class,它会起作用。
- 如果你使用 eclipse,ECJ 可以工作。
当我重写抛出的异常时发生了什么?这是 javac 错误吗?
我正在使用 jdk_1.8_112
最小化代码以重现
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;
}
}
这是一个 jdk 错误,已修复 1.8.0_171
(可能不是最低修复版本)。
为防止重复关闭,此题与this one完全不同。
假设我有以下界面
@FunctionalInterface
interface FuncE0<R, E extends Exception> {
R call() throws E;
}
它与 lambda 配合得很好
FuncE0<Integer, IOException> get() {
return () -> 1;
}
但是如果我让接口从 Callable
扩展,它就会中断。
@FunctionalInterface
interface FuncE0<R, E extends Exception> extends Callable<R> {
@Override
R call() throws E;
}
用法相同。编译器给我以下错误
JustTest.java:8: error: call() in <anonymous JustTest$> cannot implement call() in FuncE0
return () -> 1;
^ overridden method does not throw Exception
- 如果您删除
FuncE0
中的覆盖方法R call() throws E
,它将起作用。 - 如果您使用匿名 class,它会起作用。
- 如果你使用 eclipse,ECJ 可以工作。
当我重写抛出的异常时发生了什么?这是 javac 错误吗?
我正在使用 jdk_1.8_112
最小化代码以重现
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;
}
}
这是一个 jdk 错误,已修复 1.8.0_171
(可能不是最低修复版本)。