Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8350542

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

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Javadoc for java.util.Optional.orElseThrow(Supplier) misses mention of another one cause of NullPointerException - when exception supplying function returns a null result.

      In my opinion throwing of NPE when exception supplying function returns a null result is implementation-independent behavior. This case is equal to case when exception supplying function is null.

      It is worth noting that Javadocs for

      java.util.Optional.or(Supplier)
      java.util.Optional.flatMap(Function)

      clearly highlights this cause in similar cases.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Open Javadoc for java.util.Optional.orElseThrow(Supplier)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      @throws NullPointerException if no value is present and the exception supplying function is {@code null} or exception supplying function returns a null result
      ACTUAL -
      @throws NullPointerException if no value is present and the exception supplying function is {@code null}

            clanger Christoph Langer
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: