在.net中,AggregateException類允許您引發包含多個異常的異常。什麼是來自.net的AggregateException的Java等價物?
例如,如果您並行運行多個任務,並且其中一些任務因異常而失敗,您將希望拋出一個AggregateException。
java有一個等價的類嗎?
的具體情況下,我想使用它:
public static void runMultipleThenJoin(Runnable... jobs) {
final List<Exception> errors = new Vector<Exception>();
try {
//create exception-handling thread jobs for each job
List<Thread> threads = new ArrayList<Thread>();
for (final Runnable job : jobs)
threads.add(new Thread(new Runnable() {public void run() {
try {
job.run();
} catch (Exception ex) {
errors.add(ex);
}
}}));
//start all
for (Thread t : threads)
t.start();
//join all
for (Thread t : threads)
t.join();
} catch (InterruptedException ex) {
//no way to recover from this situation
throw new RuntimeException(ex);
}
if (errors.size() > 0)
throw new AggregateException(errors);
}
,我不知道一個內置。但是,我從來沒有找過一個。 – Powerlord 2010-07-30 20:08:08