lambda - Java 8 method reference unhandled exception -


i'm working on project java 8 , found 1 situation can't understand.

i have code this:

void deleteentity(node node) throws someexception {     (childnode child: node.getchildren()) {        deletechild(child);     } }  void deletechild(object child) throws someexception {     //some code } 

this code working fine, can rewrite method reference:

void deleteentity(node node) throws someexception {     node.getchildren().foreach(this::deletechild); } 

and code doesn't compile, giving error incompatible thrown types *someexception* in method reference.

also idea gave me error unhandled exception.

so, question why? why code compiles each loop , doesn't compile lambda?

if @ consumer<t> interface, accept method (which method reference using) isn't declared throw checked exceptions - therefore can't use method reference is declared throw checked exception. enhanced loop okay, because there you're in context someexception can thrown.

you potentially create wrapper converts checked exception unchecked exception, , throw that. alternatively, declare own functional interface accept() method does throw checked exception (probably parameterizing interface exception), , write own foreach method takes functional interface input.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -