Optional.orElseThrow(Supplier) does not specify behavior when supplier returns null

XMLWordPrintable

    • Type: CSR
    • Resolution: Approved
    • Priority: P4
    • 25
    • Component/s: core-libs
    • None
    • behavioral
    • minimal
    • Javadoc only. This documents a longstanding behavior that should have been specified.
    • Java API
    • SE

      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) {
      

            Assignee:
            Christoph Langer
            Reporter:
            Webbug Group
            Chen Liang, Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: