A wrapped AggregateException reports only the first exception in the ToString method

I get a full set of nested exceptions when I directly use the method ToString()on AggregateException:

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

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

The problem is that I get only the first exception when it AggregateExceptionasserts itself in another exception:

    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");
    }

ex2missing in the received string. Is this a bug or a known class function AggregateException?

+4
source share
1 answer

I think this is not a mistake. More normal behavior

The problem is that the ToString () in Exceptionwill not cause ToString() innerException, but the private ToString(bool,bool)to the class of exceptions.

ToString() AggregateException .

AggregateException innerException , .

new Exception("ex1")

+4

Source: https://habr.com/ru/post/1621418/


All Articles