首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自.net的AggregateException在java中的等价物是什么?

来自.net的AggregateException在java中的等价物是什么?
EN

Stack Overflow用户
提问于 2010-07-31 04:04:41
回答 4查看 5.1K关注 0票数 19

在.net中,AggregateException类允许抛出包含多个异常的异常。

例如,如果并行运行多个任务,并且其中一些任务因异常而失败,则可能需要抛出AggregateException。

java有没有等价的类?

我想要在以下情况下使用它:

代码语言:javascript
复制
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); 
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-07-31 04:16:29

我不知道有什么内置的或库的类,因为我以前从来没有想过要这样做(通常你只需要链接异常),但是自己编写并不难。

您可能希望选择其中一个异常作为“主要”,这样它就可以用来填充堆栈跟踪等。

代码语言:javascript
复制
public class AggregateException extends Exception {

    private final Exception[] secondaryExceptions;

    public AggregateException(String message, Exception primary, Exception... others) {
        super(message, primary);
        this.secondaryExceptions = others == null ? new Exception[0] : others;
    }

    public Throwable[] getAllExceptions() {

        int start = 0;
        int size = secondaryExceptions.length;
        final Throwable primary = getCause();
        if (primary != null) {
            start = 1;
            size++;
        }

        Throwable[] all = new Exception[size];

        if (primary != null) {
            all[0] = primary;
        }

        Arrays.fill(all, start, all.length, secondaryExceptions);
        return all;
    }

}
票数 4
EN

Stack Overflow用户

发布于 2012-05-21 01:12:40

Java7的Throwable.addSuppressed(Throwable)将做类似的事情,尽管它是为稍微不同的目的而构建的(try-with-resource)

票数 11
EN

Stack Overflow用户

发布于 2010-07-31 06:33:31

您可以将多个taska表示为

代码语言:javascript
复制
List<Callable<T>> tasks

然后,如果你想让计算机真正并行使用它们

代码语言:javascript
复制
ExecutorService executorService = .. initialize executor Service
List<Future<T>> results = executorService.invokeAll ( ) ;

现在您可以遍历结果了。

代码语言:javascript
复制
try
{
     T val = result . get ( ) ;
}
catch ( InterruptedException cause )
{
     // this is not the exception you are looking for
}
catch ( ExecutionExeception cause )
{
     Throwable realCause = cause . getCause ( ) // this is the exception you are looking for
}

因此,realCause (如果存在)是在其关联任务中抛出的任何异常。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3374966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档