当我直接在 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
不会调用 innerException
的 ToString()
,而是调用私有(private)的 ToString(bool,bool)
异常类本身的方法。
所以覆盖了ToString()未调用 AggregateException
的方法。
constructor AggregateException 的异常会将 innerException 设置为传递给构造函数的第一个异常。
在您的情况下,这是 new Exception("ex1")
关于c# - Wrapped AggregateException 仅报告 ToString 方法中的第一个异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34433446/