[concurrency-interest] CompletableFuture.whenComplete survey

Doug Lea dl at cs.oswego.edu
Mon Dec 21 13:49:12 EST 2015


On 12/21/2015 01:12 PM, joe.bowbeer at gmail.com wrote:
> In the second survey email, the only options clearly *compatible* with the
> CompletionStage spec are ...

No. Here's jdk8 spec:

"If the supplied action itself encounters an exception, then the returned stage 
exceptionally completes with this exception unless this stage also completed 
exceptionally."

Which describes options A and B. Option B also adds as suppressed
the whenComplete exception, which is not required but not disallowed
by current spec.

Options C would comply only if the "unless" clause were dropped.

Options D and E would require more changes.

where, as a reminder...

   CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
      if (true)
         throw new FirstException();
      else
         return "A";
    });

   CompletableFuture<String> f2 = f1.whenComplete((result, exception) -> {
     if (true)
        throw new SecondException();
    });

A. The FirstException. In other words, preserve the source outcome (only) if 
exceptional, ignoring the SecondException.

B. The FirstException, with the SecondException as its suppressed exception.  In 
other words, preserve but modify the source exception to record the SecondException.

C. The SecondException. In other words, replace the source outcome (whether it 
is exceptional or not).

D. The SecondException, with the FirstException as its suppressed exception.  In 
other words, replace the source outcome, but if exceptional, record it as a 
suppressedException of the SecondException.

E. A new CompletionException, with the FirstException as its cause and the 
SecondException as its suppressed exception. In other words, indicate that 
throwing an exception in whenComplete is a different form of error.





More information about the Concurrency-interest mailing list