AggregateException.Flatten 方法在 MSDN 上的文档说明如下:“如果任何内部异常本身是 AggregateException 的实例,则此方法将递归地展平所有异常。新 AggregateException 中返回的内部异常将是所有根植于提供的 AggregateException 实例的异常树的内部异常。”
这听起来很可疑,就像我们将丢失堆栈跟踪信息一样,所以我查看了Reference Source上的实现,看起来堆栈跟踪信息确实会丢失。
所以我们进行了测试:
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());
}
测试的输出如下:
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<---
请注意,扁平化的 AggregateException 中没有提到第 39 行!!!
这个丢失的信息重要吗?或者,当我们展平 AggregateException 时,我们是否会丢失第 39 行并不重要?