But*_*aur 6 c# aggregateexception
当我直接使用ToString()method 时,我得到完整的嵌套异常集AggregateException:
public void GreenTest()
{
var ex = new AggregateException(new Exception("ex1"), new Exception("ex2"));
ex.ToString()
.Should()
.Contain("ex1")
.And
.Contain("ex2");
}
Run Code Online (Sandbox Code Playgroud)
问题是当我被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");
}
Run Code Online (Sandbox Code Playgroud)
ex2在结果字符串中不存在.这是一个错误或类的一些众所周知的功能AggregateException?
我认为这不是一个错误。更多的是正常行为
问题是ToString()不会Exception调用 的ToString(),而是调用异常类本身的innerException私有方法。ToString(bool,bool)
因此不会调用重写的ToString()方法。AggregateException
AggregateException 的构造函数会将innerException 设置为传递给构造函数的第一个异常。
在你的情况下这是new Exception("ex1")
| 归档时间: |
|
| 查看次数: |
687 次 |
| 最近记录: |