[concurrency-interest] Layered exception handling with CompletableFuture

Millies, Sebastian Sebastian.Millies at softwareag.com
Wed Aug 27 04:39:30 EDT 2014


thank you for the helpful response.

It made me remember dimly some discussion of exception transparency on lambda-dev, and how it finally wasn’t possible to make it work. I do not believe this will ever be solved on the language level, and I regret that. In the same vein CompletableFuture.suppyAsync() does not accept a Callable, only a Supplier. In my situation there’s a bunch of pre-existing, exception throwing application methods (not to mention the Java library methods) that I now have a new requirement to combine asynchronously. So I find myself wrapping all the Callables, and tunneling the exceptions with RuntimeException, as Doug Lea recommends.

I have looked at your suggestion, and I’m sorry to say I do not see how it would help, unless I rewrite everything from scratch or use bayou (would need something like your Async interface).
And it also does not seem to address the one-stop exception-handling concern, because guess I would somehow need to call CompletableFuture#handle() and that can’t throw. Even in bayou, from a cursory look at Async.java, it seems that catch_() doesn’t rethrow the  exception if it is not of the appropriate class.


From: Zhong Yu [mailto:zhong.j.yu at gmail.com]
Sent: Wednesday, August 27, 2014 2:02 AM
To: Millies, Sebastian
Cc: concurrency-interest at cs.oswego.edu
Subject: Re: [concurrency-interest] Layered exception handling with CompletableFuture

[OP snipped]
Apparently, CompletableFuture does not  take too kindly to checked exceptions [1].
A lot of people even consider checked exceptions a design mistake on the language level. Nevertheless, most Java programmers and APIs do use checked exceptions, for returning alternative results, and for flow controls.
A workaround is to create your own helper method like
    static <T,X> CF<T> catch_(CF<T> cf, Class<X> exceptionType, FuncE<X,T> exceptionHandler) { ... }
where FuncE is similar to Function, except the apply() method throws `Exception`

Then you can use it like
    catch_( future, SomeException.class, ex->someValue )
    catch_( future, SomeExcpetion.class, ex->throw new AnotherException() )
That is what we did in bayou Async API [2]. The problem you posted early can be solved in bayou as
              .catch_(TimeoutException.class, ex->defaultValue);

[1] http://cs.oswego.edu/pipermail/concurrency-interest/2012-December/010486.html
[2] http://bayou.io/release/0.9/docs/async/Async_Programming.html#Sequential_Actions

Zhong Yu

Software AG – Sitz/Registered office: Uhlandstraße 12, 64297 Darmstadt, Germany – Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140827/fe82a75f/attachment-0001.html>

More information about the Concurrency-interest mailing list