RFR(s): 8140281 add no-arg Optional.orElseThrow() as preferred alternative to get()

Stuart Marks stuart.marks at oracle.com
Fri Dec 8 00:33:41 UTC 2017

Hi all,

Please review this changeset that introduces a new no-arg method orElseThrow() 
to Optional, as a preferred alternative to the get() method.

Corresponding methods are also added to OptionalDouble, Int, and Long.

The orElseThrow() method is functionally identical to get(). It has some 
affinity with the existing orElseThrow(exceptionSupplier) method, being 
equivalent to orElseThrow(NoSuchElementException::new).

The get() method is functionally unchanged. It is NOT being deprecated, although 
some wording in the doc has been added to point to orElseThrow() as the 
"preferred alternative." This is part of a (slow) migration away from 
Optional.get(), which has an obvious, attractive name that is often misused. 
These issues have been discussed on this list previously:


Please note that much of that discussion was about the then-proposed deprecation 
of Optional.get(), which is NOT part of this proposal. There are no plans to 
deprecate Optional.get() at this time. This proposal ONLY introduces a new 
method orElseThrow() that is identical in function to get().

Bug report:






More information about the core-libs-dev mailing list