c# - Wrapped AggregateException 仅报​​告 ToString 方法中的第一个异常

标签 c# aggregateexception

当我直接在 AggregateException 上使用 ToString() 方法时,我得到了一组完整的嵌套异常:

public void GreenTest()
{
    var ex = new AggregateException(new Exception("ex1"), new Exception("ex2"));

    ex.ToString()
        .Should()
        .Contain("ex1")
        .And
        .Contain("ex2");
}

问题是当 AggregateException 包含在另一个异常中时,我只得到第一个异常:

public void RedTest()
{
    var ex = new Exception("wrapper", new AggregateException(new Exception("ex1"), new Exception("ex2")));

    ex.ToString()
        .Should()
        .Contain("wrapper")
        .And
        .Contain("ex1")
        .And
        .Contain("ex2");
}

ex2 不在结果字符串中。这是 AggregateException 类的错误还是一些众所周知的功能?

最佳答案

我认为这不是一个错误。更正常的行为

问题是 ToString() on Exception 不会调用 innerExceptionToString(),而是调用私有(private)的 ToString(bool,bool)异常类本身的方法。

所以覆盖了ToString()未调用 AggregateException 的方法。

constructor AggregateException 的异常会将 innerException 设置为传递给构造函数的第一个异常。

在您的情况下,这是 new Exception("ex1")

关于c# - Wrapped AggregateException 仅报​​告 ToString 方法中的第一个异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34433446/

相关文章:

c# - WPF 切换面板可见性

c# - Task.WaitAll 索引越界异常原因

AggregateException 中的 C# InnerException

c# - 对 AggregateException Handle 方法的混淆

c# - 如何修复 "' System.AggregateException' 出现在 mscorlib.dll 中”

c# 获取或设置预期的访问器

c# - 十六进制值 0x00,是无效字符 : still encounter problem after replace\0 with ""

c# - 添加自定义属性时如何删除 CS0649 编译器警告?

c# - AggregateException 中的 TaskCanceledException 不包含堆栈跟踪

c# - 异步/等待 vs ContinueWith