For comparison, is this try-catch version the equivalent to your
isSuccess version?
Preconditions.checkState(future.isDone());
try {
T value = future.get();
// Use value.
} catch (CancellationException|ExecutionException|InterruptedException e) {
System.err.println("Well that's too bad...");
}
That is indeed a pretty ugly catch() block. One way to shrink it is to
use Futures.get:
Preconditions.checkState(future.isDone());
try {
T value = Futures.get(future, MyException.class);
// Use value.
} catch (MyException e) {
System.err.println("Well that's too bad...");
}
Another feature we've considered adding is Futures.getNow, which would
mean you'd no longer have to deal with InterruptedException. Perhaps
it would also be OK for CancellationException to be propagated? That
would leave you with a single kind of exception to catch:
Preconditions.checkState(future.isDone());
try {
T value = Futures.getNow(future.get);
// Use value.
} catch (ExecutionException e) {
System.err.println("Well that's too bad...");
}
There's only so much we can do with that pesky ExecutionException around :\