您能解释一下为什么必须从 lambda 表达式中捕获已检查的异常吗?换句话说,为什么下面的代码无法编译...
public void doSomething(ObjectInputStream istream) throws IOException {
// The read method throws an IOException.
IntStream.range(0, 10).forEach(i -> someList.add(read(istream)));
}
但是这个会吗?
public void doSomething(ObjectInputStream istream) throws IOException {
IntStream.range(0, 10).forEach(i -> {
try {
// The read method throws an IOException.
someList.add(read(istream));
}
catch (IOException ioe) {
// Callee has to handle checked exception, not caller.
}
});
}
似乎被调用者现在必须处理抛出的任何检查异常,而不是调用者。