我运行了一些代码CompletableFuture
并想处理调用它的代码中的错误。但是 catch 不起作用,我做错了什么?
try {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
throw new IllegalStateException("Error");
});
} catch (IllegalStateException e) {
System.out.println("Expected Error: " + e.getMessage());
}
附言:我知道supplyAsync
和exceptionally
,但这个选项不适合我