Hacker News new | ask | show | jobs
by masklinn 3710 days ago
> I think the options orElse/orElseGet and orElseThrow are enough to replace every get() method, and they'll force you to think about the missing scenario.

orElseThrow doesn't force you to think about the missing scenario, only to think about which exception you want to throw, which in many case you don't care for.

If orElseThrow had an override throwing NoSuchElementException by default it would be a perfect replacement, alas it does not.

1 comments

Of course it does, you actively choose to throw something! That is, in some cases, perfectly acceptable. All three methods tell you there is something else in case you don't have a value in the optional. get() doesn't.