CompletableFutureにexceptional()にsupplyAsync()例外を処理させる
Nov 20 2020
質問はかなり単純です:私はCompletableFuture#exceptionally
と一緒に使用するエレガントな方法を探していCompletableFuture#supplyAsync
ます。これは機能しないものです:
private void doesNotCompile() {
CompletableFuture<String> sad = CompletableFuture
.supplyAsync(() -> throwSomething())
.exceptionally(Throwable::getMessage);
}
private String throwSomething() throws Exception {
throw new Exception();
}
背後exceptionally()
にある考え方Exception
は、投げられた場合を正確に処理することだと思いました。しかし、私がこれを行うと、それは機能します:
private void compiles() {
CompletableFuture<String> thisIsFine = CompletableFuture.supplyAsync(() -> {
try {
throwSomething();
return "";
} catch (Exception e) {
throw new RuntimeException(e);
}
}).exceptionally(Throwable::getMessage);
}
私はそれで作業することができましたが、それはひどく見え、物事を維持するのが難しくなります。すべての変換必要としないこの清潔に保つための方法はありませんException
にはRuntimeException
?
回答
2 Eugene Nov 21 2020 at 02:21
これはあまり人気のないライブラリかもしれませんが、内部で使用しています(そして、時々私もそこでいくつかの作業を行いますが、マイナーですが):NoException。それは本当に、本当に私の好みのためにうまく書かれています。これはそれが持っている唯一のものではありませんが、間違いなくあなたのユースケースをカバーしています:
サンプルは次のとおりです。
import com.machinezoo.noexception.Exceptions;
import java.util.concurrent.CompletableFuture;
public class SO64937499 {
public static void main(String[] args) {
CompletableFuture<String> sad = CompletableFuture
.supplyAsync(Exceptions.sneak().supplier(SO64937499::throwSomething))
.exceptionally(Throwable::getMessage);
}
private static String throwSomething() throws Exception {
throw new Exception();
}
}
または、これらを自分で作成することもできます。
final class CheckedSupplier<T> implements Supplier<T> {
private final SupplierThatThrows<T> supplier;
CheckedSupplier(SupplierThatThrows<T> supplier) {
this.supplier = supplier;
}
@Override
public T get() {
try {
return supplier.get();
} catch (Throwable exception) {
throw new RuntimeException(exception);
}
}
}
@FunctionalInterface
interface SupplierThatThrows<T> {
T get() throws Throwable;
}
そして使用法:
CompletableFuture<String> sad = CompletableFuture
.supplyAsync(new CheckedSupplier<>(SO64937499::throwSomething))
.exceptionally(Throwable::getMessage);