The documentation on MSDN for the AggregateException.Flatten method says the following: "If any inner exceptions are themselves instances of AggregateException, this method will recursively flatten all of them. The inner exceptions returned in the new AggregateException will be the union of all of the the inner exceptions from exception tree rooted at the provided AggregateException instance."
This sounds suspiciously like we will be losing stack trace info, so I took a look at the implementation on Reference Source, and it does appear that stack trace information would be lost.
So we ran a test:
try
{
try
{
try
{
throw new InvalidOperationException();
}
catch (Exception e)
{
try
{
throw new FormatException();
}
catch (Exception e2)
{
throw new AggregateException(e, e2);
}
}
}
catch (Exception e)
{
try
{
throw new NotImplementedException();
}
catch (Exception e2)
{
throw new AggregateException(e, e2);
}
}
}
catch (Exception e)
{
File.AppendAllText(@"C:\tmp.txt", e.ToString());
File.AppendAllText(@"C:\tmp.txt", ((AggregateException)e).Flatten().ToString());
}
The output of the test is as follows:
System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 29
--- End of inner exception stack trace ---
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 39
--- End of inner exception stack trace ---
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 51
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 29
--- End of inner exception stack trace ---
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 39
---> (Inner Exception #0) System.InvalidOperationException: Operation is not valid due to the current state of the object.
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 29<---
---> (Inner Exception #1) System.FormatException: One of the identified items was in an invalid format.
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 35<---
<---
---> (Inner Exception #1) System.NotImplementedException: The method or operation is not implemented.
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 47<---
System.AggregateException: One or more errors occurred. ---> System.NotImplementedException: The method or operation is not implemented.
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 47
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.NotImplementedException: The method or operation is not implemented.
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 47<---
---> (Inner Exception #1) System.InvalidOperationException: Operation is not valid due to the current state of the object.
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 29<---
---> (Inner Exception #2) System.FormatException: One of the identified items was in an invalid format.
at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 35<---
Notice that line 39 is not mentioned in the flattened AggregateException!!!
Is this lost info important? Or is there a reason that it doesn't matter that we'd be losing line number 39 when we flatten the AggregateException?