什么是Java的等效AggregateException从.NET? [英] What is the java equivalent of AggregateException from .net?

查看:103
本文介绍了什么是Java的等效AggregateException从.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中AggregateException类可以抛出包含多个异常异常。

例如,你想抛出一个AggregateException如果你在并行运行多个任务,他们中的一些失败的例外。

Java的是否有一个等价类?

具体的情况下,我想使用它:

 公共静态无效runMultipleThenJoin(Runnable的...作业){
    最后的名单,其中,异常和GT;错误=新矢量<异常>();
    尝试 {
        //创建异常处理线程作业每个作业
        名单<主题>线程=新的ArrayList<螺纹>();
        对于(最终可运行作业:作业)
            threads.add(新主题(新的Runnable(){公共无效的run(){
                尝试 {
                    job.run();
                }赶上(例外前){
                    errors.add(前);
                }
            }}));

        //启动所有
        对(线程t:线程)
            t.start();

        //加入所有
        对(线程t:线程)
            t.join();
    }赶上(InterruptedException的前){
        //无法从这种情况下恢复
        抛出新的RuntimeException(前);
    }

    如果(errors.size()大于0)
        抛出新AggregateException(错误);
}
 

解决方案

我不知道任何内置或库类的,因为我从来没有,即使想之前(通常你只想链做到这一点的例外),但它不会那么难写自己。

你可能想选择一个例外是主,所以它可以用来填补踪迹等。

 公共类AggregateException扩展异常{

    私人最终例外[] secondaryExceptions;

    公共AggregateException(字符串消息,除初级,异常......等){
        超(消息,初级);
        this.secondaryExceptions =其他== NULL?新的异常[0]:人;
    }

    市民可抛出[] getAllExceptions(){

        INT开始= 0;
        INT大小= secondaryExceptions.length;
        最后的Throwable主=的getCause();
        如果(主!= NULL){
            开始= 1;
            尺寸++;
        }

        Throwable的[]所有=新的异常【尺寸】;

        如果(主!= NULL){
            所有的[0] =小学;
        }

        Arrays.fill(全部启动,all.length,secondaryExceptions);
        返回所有;
    }

}
 

In .net the AggregateException class allows you to throw an exception containing multiple exceptions.

For example, you would want to throw an AggregateException if you ran multiple tasks in parallel and some of them failed with exceptions.

Does java have an equivalent class?

The specific case I want to use it in:

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); 
}

解决方案

I'm not aware of any built-in or library classes, as I've never even though of wanting to do this before (typically you would just chain the exceptions), but it wouldn't be that hard to write yourself.

You'd probably want to pick one of the Exceptions to be "primary" so it can be used to fill in stacktraces, etc.

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;
    }

}

这篇关于什么是Java的等效AggregateException从.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆