[concurrency-interest] CompletableFuture.whenComplete survey

Tim Peierls tim at peierls.net
Sat Dec 19 11:40:47 EST 2015


On Sat, Dec 19, 2015 at 11:16 AM, Chris Purcell <chris.purcell.39 at gmail.com>
wrote:

> The second option surprised me, as we now have a fourth approach to
> suppressed exceptions in Java: try/finally (throw away A),
> try-with-resources (suppress B), j8 whenComplete (throw away B) and j9+
> whenComplete (suppress A). Isn't this unnecessarily complicated?
>
Like Joe, I would have voted against suppression if that had been given as
a survey option.


> Why not behave like a finally block, and let the user add A as a
> suppressed exception if they want it?
>
Is this opt-in behavior not achievable with a wrapper? (Assuming
survey-option-2-without-suppression semantics.)

    public static <T> BiConsumer<T, Throwable> suppressEarlierException(
            BiConsumer<? super T, ? super Throwable> action) {

        return (r1, ex1) -> {
            try {
                action.accept(r1, ex1);
            } catch (Throwable ex2) {
                ex2.addSuppressed(ex1);
                throw ex2;
            }
        };
    }


> WhenComplete seems like the only way to reliably close resources in async
> code, so I expected it to work like try-with-resources. That seems to be
> the point of contention for others too. Perhaps there should be another
> method for this use-case instead? One which takes a simple runnable and
> suppresses any exception it throws.
>
I don't see why CF.handle(BiFunction) isn't already sufficient for
providing any behavior you want here.

--tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20151219/ba80c87d/attachment.html>


More information about the Concurrency-interest mailing list