Flattening of AggregateExceptions for Processing

前端 未结 5 823
暖寄归人
暖寄归人 2020-12-10 01:46

I\'m running into a few issues where I call flatten on an AggregateException, but inside there is still ANOTHER AggregateException! T

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 02:13

    Yes, there's exactly what you're asking for:

    AggreggateException.Flatten()
    

    will go through and compress everything down to a single AggregateException. So you can use it to loop through the all the inner exceptions like this:

    try
    {
        // something dangerous
    }
    catch (AggregateException ae)
    { 
        foreach(var innerException in ae.Flatten().InnerExceptions)
        {
            // handle error
        }
    }
    

    MSDN link: http://msdn.microsoft.com/en-us/library/system.aggregateexception.flatten.aspx

提交回复
热议问题