请问为什么在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.
}
});
}
似乎现在被调用者必须处理抛出的已检查异常,而不是调用者。