在 .net 中,AggregateException 类允许您抛出包含多个异常的异常。
例如,如果您并行运行多个任务并且其中一些任务因异常而失败,您可能希望抛出 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);
}
最佳答案
Java 7 Throwable.addSuppressed(Throwable)会做类似的事情,尽管它是为稍微不同的目的而构建的(try-with-resource)
关于java - .net 中 AggregateException 的 java 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3374966/