gpt4 book ai didi

c# - AggregateException.Flatten() 丢失堆栈跟踪信息。丢失的信息重要还是不重要?

转载 作者:行者123 更新时间:2023-11-30 12:44:03 26 4
gpt4 key购买 nike

文档 on MSDN对于 AggregateException.Flatten 方法,说明如下:“如果任何内部异常本身就是 AggregateException 的实例,则此方法将递归地展平所有这些异常。新 AggregateException 中返回的内部异常将是所有内部异常的并集以提供的 AggregateException 实例为根的异常树。”

这听起来很可疑,好像我们会丢失堆栈跟踪信息,所以我看了一下实现 on 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 行无关紧要?

最佳答案

如果我正确理解您的示例,您是说在抛出 AggregateException 时创建的堆栈跟踪丢失了。这是预期的,因为 Flatten() 将过滤掉属于 InnerExceptions 层次结构的任何 AggregateExceptions。我不会说信息丢失了,因为在大多数情况下有用的信息将在非 AggregateException 实例中。

关于c# - AggregateException.Flatten() 丢失堆栈跟踪信息。丢失的信息重要还是不重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29526112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com