If a is Optional[123] and b is Optional[empty].
a.orElse(b.orElseThrow(() -> new UnexpectedInternalException(
"Error")))
Why does it throw?
If a is Optional[123] and b is Optional[empty].
a.orElse(b.orElseThrow(() -> new UnexpectedInternalException(
"Error")))
Why does it throw?
Because parameters are evaluated first. This has nothing to do with Optional...
orElse()
is a method, and before that method is executed, its arguments are evaluated. In this case the evaluation of the argument (b.orElseThrow(() -> new UnexpectedInternalException("Error"))
) throws an exception.