Kotlin-Demo/bunny/async/CompletableFutureTest7.java

22 lines
655 B
Java

package bunny.async;
import java.util.concurrent.CompletableFuture;
public class CompletableFutureTest7 {
public static void main(String[] args) {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
// 模拟一个抛出异常的任务
throw new RuntimeException("Task failed");
});
CompletableFuture<Integer> handledFuture = future.exceptionally(ex -> {
System.out.println("Exception 消息是什么呢?: " + ex.getMessage());
return 0;
});
int result = handledFuture.join();
System.out.println("Result: " + result);
}
}