Summary
Javadoc for java.util.Optional.orElseThrow(Supplier)
should be amended to mention that it will throw a NullPointerException when the Supplier function returns null as a result. Similar to the Javadoc for java.util.Optional.or(Supplier)
.
Problem
Currently, Javadoc for java.util.Optional.orElseThrow(Supplier) says:
@throws NullPointerException if no value is present and the exception supplying function is {@code null}
Solution
Javadoc for java.util.Optional.orElseThrow(Supplier) should state:
@throws NullPointerException if no value is present and the exception supplying function is {@code null} or produces a {@code null} result
Specification
diff --git a/src/java.base/share/classes/java/util/Optional.java b/src/java.base/share/classes/java/util/Optional.java
index 21128fa980d..6c4b5e2451c 100644
--- a/src/java.base/share/classes/java/util/Optional.java
+++ b/src/java.base/share/classes/java/util/Optional.java
@@ -393,8 +393,8 @@ public T orElseThrow() {
* exception to be thrown
* @return the value, if present
* @throws X if no value is present
* @throws NullPointerException if no value is present and the exception
- * supplying function is {@code null}
+ * supplying function is {@code null} or produces a {@code null} result
*/
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (value != null) {
- csr of
-
JDK-8350542 Optional.orElseThrow(Supplier) does not specify behavior when supplier returns null
-
- Resolved
-