CompletableFuture 체인을 가지고 놀고있어 (적어도 나를 위해) 예기치 않은 비헤이비어가있는 상황을 발견했습니다. .thenCompose()
호출에서 예외적 인 CompletableFuture가 전달 된 경우 원래 CompletableFuture가 래핑 된 원래 예외로 완료됩니다 CompletionException
. 예를 들어없이 이해하기 어려울 수 있습니다 : 물론원하는 CompletableFuture 비헤이비어 적용
public static <T> CompletableFuture<T> exceptional(Throwable error) {
CompletableFuture<T> future = new CompletableFuture<>();
future.completeExceptionally(error);
return future;
}
public static void main(String[] args) {
CompletableFuture<Void> exceptional = exceptional(new RuntimeException());
exceptional
.handle((result, throwable) -> {
System.out.println(throwable);
// java.lang.RuntimeException
System.out.println(throwable.getCause());
// null
return null;
});
CompletableFuture
.completedFuture(null)
.thenCompose(v -> exceptional)
.handle((result, throwable) -> {
System.out.println(throwable);
// java.util.concurrent.CompletionException: java.lang.RuntimeException
System.out.println(throwable.getCause());
// java.lang.RuntimeException
return null;
});
}
내가 상관없이 전이나 체인 후에 얼마나 많은 변환 같은 RuntimeException
처리하지 기대했다. 두 가지 질문이 있습니다.
- 예상되는 동작입니까?
- 수동 언 랩핑을 제외하고 원래 예외가 계속 유지되는 옵션이 있습니까?
thenCompose()
위한