I have encountered and contributed to projects which use Java8 streams and lambdas extensively. The code is littered with blocks like this:
... some other code ... -> {
try {
... some more code ...
}
catch(SomeException e) {
throw new RuntimeException(e);
}
} ...
or alternatively
private void someMethodWhichWillBeCalledInALambda(...) {
try {
... some code ...
}
catch(SomeException e) {
throw new RuntimeException(e);
}
}
In addition to littering the code with these extra try/catch blocks, we also lose the ability to do proper exception handling up the stack (in the callers of the code that uses lambdas).
Lambdas break the long-standing Java best practice of propagating exceptions up the stack to the nearest block of code capable of handling it meaningfully. I cannot quote an authoritative source for this best practice at the moment, perhaps someone can point to it in the comments. In these examples, the code in the lambda is too low-level to meaningfully handle the exception.
My question is, what is the alternative to this mess?
In keeping with SO rules, please limit your answers only to working code, although I wouldn't mind in the comments some bigger ideas... such as possible enhancements to the Java language and how to get a proposal sent through the JCP, if one doesn't already exist.