我需要將運行時例外從 CompletableFuture Exception 塊重新拋出到父方法,但看起來 runtimeException 正在例外塊本身記錄,而不是傳播回父方法。誰能建議/建議我在這里做錯了什么,或者這是正確的投擲方式嗎?我有一個如下的函式呼叫:
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);
}
);
}
uj5u.com熱心網友回復:
呼叫代碼不會等待 Future 完成或獲取狀態。這是需要添加的核心內容。
CompletableFuture<Void> future = asyncTask.process(msg)
.thenAccept(this::postProcessTasks);
try {
future.get();
} catch (ExecutionException e) {
Throwable asyncException = e.getCause();
// .. do something here
}
如果您打算將異步例外傳播回呼叫者,那么whenComplete可能不是要使用的呼叫;它旨在處理異步呼叫中的例外。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442712.html
上一篇:無法在VSCode中克隆存盤庫
