Having following scratch code:
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> process1 = CompletableFuture.runAsync(() -> {
System.out.println("Process 1 with exception");
throw new RuntimeException("Exception 1");
});
CompletableFuture<Void> process2 = CompletableFuture.runAsync(() -> {
System.out.println("Process 2 without exception");
});
CompletableFuture<Void> process3 = CompletableFuture.runAsync(() -> {
System.out.println("Process 3 with exception");
throw new RuntimeException("Exception 3");
});
CompletableFuture<Void> allOfProcesses = CompletableFuture.allOf(process1, process2, process3);
allOfProcesses.get();
}
I'm looking for way how to collect all exceptions which have been thrown during parallel execution in CompletableFuture.allOf()
and map it to List.
I know I can do this by returning exception (CompletableFuture<Exception>
) instead of throwing and collect it by using CompletableFuture::join
but I think throwing exception approach is better than returning and throwing it later