Can you please explain why checked exceptions have to be caught from within lambda expressions? In other words, why does the following code not compile...
public void doSomething() throws IOException {
// The read method throws an IOException.
IntStream.range(0, 10).forEach(i -> someList.add(read(istream)));
}
but this one will?
public void doSomething() 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.
}
});
}
It seems like the callee now has to handle any checked exceptions that are thrown and not the caller.
Aucun commentaire:
Enregistrer un commentaire