一,使用了有返回值的CompletableFuture
如果使用了有返回值的CompletableFuture,想要在CompletableFuture外层,拿到异步执行的异常信息,就必须调用join()获取get()这样的阻塞方法,才可以在主线程拿到,否则,只能在CompletableFuture中通过exceptionally打印异常信息
二,使用没有返回值的CompletableFuture
如果使用了没有返回值的CompletableFuture,那么是没有办法在主线程中打印执行异步操作的异常信息的,只能通过exceptionally打印异常信息
TaskExecutor bean = SpringContextUtil.getBean("threadPoolTaskExecutor1", TaskExecutor.class);
try {
CompletableFuture<Object> exceptionally = CompletableFuture.supplyAsync(() -> {
throw new BusinessException("DSDS", 11111);
}, bean).exceptionally(e -> {
log.error(e.getMessage());
e.printStackTrace();
return null;
});
exceptionally.join();
} catch (Exception e) {
System.err.println(e.getClass().getTypeName());
log.error(e.getMessage());
}