通常在運行Future
並等待結果時,我只能捕獲InterruptedException | ExecutionException
。如何捕捉期貨的自定義例外?
但是如果任務拋出一個CustomException
,我想明確地捕獲它呢?我能做比檢查e.getCause() instanceof CustomException
更好嗎?
List<Future> futures; //run some task
for (Future future : futures) {
try {
future.get(); //may throw CustomException
} catch (InterruptedException | ExecutionException e) {
if (e.getCause() instanceof CustomException) {
//how to catch directly?
}
}
}
不,'的Future.get()'只能扔掉那些兩種例外明確。不再。 – membersound