如何从 CompletableFuture.join() 获取原始异常?

How to get original exception from CompletableFuture.join()?

有什么方法可以获取当任务执行因异常而失败时抛出的原始异常?看起来 CompletableFuture.join 总是在失败时抛出 CompletionException。

我们的原始异常包含额外的属性(成员变量),必须使用这些属性以正确的方式处理异常。

来自the docs

To better conform with the use of common functional forms, if a computation involved in the completion of this CompletableFuture threw an exception, this method throws an (unchecked) CompletionException with the underlying exception as its cause.

换句话说,

Exception original = completableException.getCause();