I have two functions throwing Exception:
public void foo() throws Exception {
// something
}
public void bar() throws Exception {
// something
}
If I combine those function calls using curly braces in lambda expression, it needs try/catch to handle exception.
public void someFunction() throws Exception {
someList.forEach(e -> {
// Show compile error and need try/catch
foo();
bar();
});
}
However, if I combine in for-loop, it is fine.
public void someFunction() throws Exception {
for (SomeElement e : someList) {
foo();
bar();
}
}
I though try/catch needed because of new closure created (using bracket), but in for-loop, it does not require. I solved it by just using for loop, but I wonder why it happens.