捕获AggregateException [英] Catching AggregateException

查看:152
本文介绍了捕获AggregateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想抛出,赶上了AggregateException。
我没有使用异常C#的非常多,但我发现的行为是有点令人惊讶。

I am trying to throw and catch an AggregateException. I did not use exceptions very much on C#, but the behaviour I found is a little bit surprising.

我的代码是:

var numbers = Enumerable.Range(0, 20);

try
{
    var parallelResult = numbers.AsParallel()
        .Where(i => IsEven(i));
    parallelResult.ForAll(e => Console.WriteLine(e));

}
catch (AggregateException e)
{
    Console.WriteLine("There was {0} exceptions", e.InnerExceptions.Count());
}



这是调用函数ISEVEN

It is calling the function IsEven

private static bool IsEven(int i)
{
    if (i % 10 == 0)
        throw new AggregateException("i");
    return i % 2 == 0;
}

这引发AggregateException。

That throws the AggregateException.

我希望代码写在0.20范围内每个偶数和有1例外两次。

I would expect the code to write every even number in the 0,20 range and "There was 1 exceptions" twice.

我得到的是一些数字印刷(他们的ForAll随机原因),然后抛出异常,但不能逮住和程序停止。

What I get is some numbers printed (they are random cause of ForAll) and then the exception is thrown, but not catched and the programs stop.

我这么想吗?

推荐答案

这实际上是样的有趣。我认为这个问题是您使用 AggregateException 以意想不到的方式,这是造成PLINQ代码中的错误。

This is actually kind of interesting. I think the problem is that you're using AggregateException in an unexpected way, which is causing an error inside the PLINQ code.

AggregateException 的整个要点是组可同时(或接近)发生一起多个异常的并行处理。因此, AggregateException 预计至少有一个内部异常。但是你扔新AggregateException(I),它没有内部异常。该PLINQ代码试图检查 InnerExceptions 属性,命中了一些错误(可能是 NullPointerException异常),然后它似乎进入某种循环。这可以说是在PLINQ中的错误,因为你正在使用一个有效的构造 AggregateException ,哪怕是一个不寻常的。

The entire point of AggregateException is to group together multiple exceptions that may occur simultaneously (or nearly so) in a parallel process. So AggregateException is expected to have at least one inner exception. But you're throwing new AggregateException("i"), which has no inner exceptions. The PLINQ code tries to examine the InnerExceptions property, hits some sort of error (probably a NullPointerException) and then it seems to go into a loop of some sort. This is arguably a bug in PLINQ, since you're using a valid constructor for AggregateException, even if it is an unusual one.

由于其他地方所指出的,扔的ArgumentException 会更语义正确。但你可以得到你抛出一个正确的构造 AggregateException ,例如通过改变 ISEVEN 函数是这样的:

As pointed out elsewhere, throwing ArgumentException would be more semantically correct. But you can get the behavior you're looking for by throwing a correctly-constructed AggregateException, for example by changing the IsEven function to something like this:

private static bool IsEven(int i)
{
    if (i % 10 == 0){
        //This is still weird
        //You shouldn't do this. Just throw the ArgumentException.
        throw new AggregateException(new ArgumentException("I hate multiples of 10"));
    }
    return i % 2 == 0;
}



我觉得这个故事的寓意是不要扔 AggregateException ,除非你真的知道自己在做什么,特别是如果你已经是一个平行的内部或工作某种形式的基于操作

I think the moral of the story is to not throw AggregateException unless you really know exactly what you're doing, particularly if you're already inside a parallel or Task-based operation of some kind.

这篇关于捕获AggregateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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