My approach would be to sneakily throw it from the lambda, but take care to have the send
method declare it in its throws
clause. Using the Exceptional
class I posted here:
public Server send(String message) throws IOException {
sessions.parallelStream()
.map(Session::getBasicRemote)
.forEach(basic -> Exceptional.from(() -> basic.sendText(message)).get());
return this;
}
This way you're effectively making the compiler "look away" for just a bit, disabling its exception checking at one spot in your code, but by declaring the exception on your send
method, you restore the regular behavior for all its callers.