我想我会在这段代码中丢失有关特定异常类型的信息。如何保留 Exception 的类型,以便不必将通用 Exception throws 子句添加到包含以下代码的方法中?我认为这与正确使用泛型有关。指导将不胜感激。
for( int i = 0; i < retries; i ++ ) {
Exception anyException = null;
try {
Future<Object> returnedObj = threadPool.submit(task);
toReturn = returnedObj.get(timeout, timeunit);
break;
} catch (RejectedExecutionException ex) {
anyException = ex;
} catch (NullPointerException ex ) {
anyException = ex;
}
...
finally {
...
if(i == retries -1 && anyException != null) {
throw anyException;
}
}
}