The MSDN documentation for the AggregateException.Flatten method says the following: "If any internal exceptions themselves are instances of AggregateException, this method will recursively smooth all of them. The internal exceptions returned in the new AggregateException value will be the union of all internal exceptions from the exception tree, embedded in the provided instance of AggregateException. "
This sounds suspicious, since we will lose information about the stack trace, so I looked at the implementation in the reference source , and it appears this information about the stack trace will be lost.
So, we performed the 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 test output 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<---
Note that line 39 is not mentioned in the smoothed AggregateException !!!
Is this lost information important? Or is there a reason that it doesn’t matter that we lose line number 39 when we catch the AggregateException?
source
share