如何从 CompletableFuture.whenComplete 异常块中抛出 RuntimeExcpetions?

How To throw RuntimeExcpetions from CompletableFuture.whenComplete exception block?

我需要将运行时异常从 CompletableFuture Exception 块重新抛出到父方法,但看起来 runtimeException 正在异常块本身中记录,而不是传播回父方法。任何人都可以请 advise/suggest 我在这里做错了什么,或者这是正确的投掷方式吗? 我有一个如下所示的函数调用:

void method1(String msg)
{
   try
   {
    method2(msg);
    }
    catch(RuntimeException e)
    {
       throw new CustomException("");
     }
}
void method2(String msg)
{
  CompletableFuture<Void> future = asyncTask.process(msg);
  future.whenComplete((res,ex) ->
        if(ex != null)
        {
           log.error("");
           throw RuntimeException("abc");
         }
        else
         { postProcessTasks(msg);
         }
       );
}

调用代码不等待 Future 完成或获取状态。下面是需要补充的核心内容

CompletableFuture<Void> future = asyncTask.process(msg)
  .thenAccept(this::postProcessTasks);
try {
  future.get();
} catch (ExecutionException e) {
  Throwable asyncException = e.getCause();
  // .. do something here
}

如果您打算将异步异常传播回调用者,那么 whenComplete 可能不是要使用的调用;它旨在处理异常 异步调用中。