2

If a is Optional[123] and b is Optional[empty].

a.orElse(b.orElseThrow(() -> new UnexpectedInternalException(
                    "Error")))

Why does it throw?

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Jor
  • 659
  • 7
  • 11
  • 4
    What did you expect it to do? The `orElseThrow()` method is explicity designed to throw if your `Optional` is absent... – fge Aug 01 '16 at 11:55

2 Answers2

5

Because parameters are evaluated first. This has nothing to do with Optional...

Daniel David Kovacs
  • 226
  • 1
  • 2
  • 13
5

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.

Eran
  • 387,369
  • 54
  • 702
  • 768