Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AggregateException - What does AggregateException.Flatten().InnerException represent?

I've been looking at some code in one of our applications that looks as follows:

catch (AggregateException ae)
{
    this._logger.Log(ae.Flatten().InnerException.ToString(), Category.Exception, Priority.High);
}

My question is this. I know what AggregateException.Flatten() does, and I know what AggregateException.Flatten().InnerExceptions represents. However, what does AggregateException.Flatten().InnerException (single) represent?

like image 298
Randy Minder Avatar asked Nov 26 '12 18:11

Randy Minder


1 Answers

It will just be one of the non-AggregateExceptions within the original exception. So for example, if you have an initial AggregateException of:

AggregateException
    AggregateException
        IOException("x")
        IOException("y")
    IOException("z")

Then the InnerException of the flattened result would come out as either IOException("x"), IOException("y") or IOException("z"). I don't believe there's any guarantee about which it would be. (I believe the current behaviour would give the "z" version at the moment...) It will be the first of the InnerExceptions on the flattened version, but that should be seen as a union of all the original non-AggregateExceptions, with no guaranteed order.

Basically InnerException isn't terribly useful for AggregateException. It would be far more useful to log all the InnerExceptions... or just call ToString() on the top-level exception, which would keep all the structure...

like image 188
Jon Skeet Avatar answered Sep 30 '22 00:09

Jon Skeet