最佳答案
I'm working on project with Java 8 and found one situation which I can't understand.
I have code like this:
void deleteEntity(Node node) throws SomeException {
for (ChildNode child: node.getChildren()) {
deleteChild(child);
}
}
void deleteChild(Object child) throws SomeException {
//some code
}
This code is working fine, but I can rewrite it with a method reference:
void deleteEntity(Node node) throws SomeException {
node.getChildren().forEach(this::deleteChild);
}
And this code doesn't compile, giving the error Incompatible thrown types *SomeException* in method reference
.
Also IDEA gave me the error unhandled exception
.
So, my question is why? Why code compiles with for each loop and doesn't compile with lambda?