Skip to content

AggregateException.ToString() when wrapped in another exception loses data #28818

@hannasm

Description

@hannasm

When AggregateException is wrapped in another exception, the ToString() method no longer populates the complete list of exception details.

Full MSTest code:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace ExceptionAggregateTest
{
    [TestClass]
    public class ExceptionHandlingTests
    {
        public Exception[] CollectExceptions() {
          var result = new Exception[2];
          try {
            throw new Exception("Exception 1");
          } catch (Exception eError) {
            result[0] = eError;
          }
          try {
            throw new Exception("Exception 2");
          } catch (Exception eError) {
            result[1] = eError;
          }
          return result;
        }
        public AggregateException CreateAggregate(Exception[] inner) {
          try {
            throw new AggregateException(inner);
          } catch (AggregateException eError) {
            return eError;
          }
        }

        public Exception CreateOuter(AggregateException inner) {
          try {
            throw new Exception("outer", inner);
          } catch (Exception eError) {
            return eError;
          }
        }

        [TestMethod]
        public void Test003() {
          var ex = CreateAggregate(CollectExceptions());
          Console.Error.Write(ex.ToString());
          Assert.Fail();
        }
        [TestMethod]
        public void Test004() {
          var ex = CreateOuter(CreateAggregate(CollectExceptions()));
          Console.Error.Write(ex.ToString());
          Assert.Fail();
        }
    }
}

Test003 (which does not wrap AggregateException) displays:

System.AggregateException: One or more errors occurred. (Exception 1) (Exception 2) ---> System.Exception: Exception 1
at ExceptionAggregateTest.ExceptionHandlingTests.CollectExceptions() in /home/hannasm/tmp_exc_test/ExceptionHandlingTests.cs:line 16
--- End of inner exception stack trace ---
at ExceptionAggregateTest.ExceptionHandlingTests.CreateAggregate(Exception[] inner) in /home/hannasm/tmp_exc_test/ExceptionHandlingTests.cs:line 29
---> (Inner Exception #0) System.Exception: Exception 1
at ExceptionAggregateTest.ExceptionHandlingTests.CollectExceptions() in /home/hannasm/tmp_exc_test/ExceptionHandlingTests.cs:line 16<---

---> (Inner Exception dotnet/corefx#1) System.Exception: Exception 2
at ExceptionAggregateTest.ExceptionHandlingTests.CollectExceptions() in /home/hannasm/tmp_exc_test/ExceptionHandlingTests.cs:line 21<---

Test004 however, (which wraps AggregateException in an Exception) is missing the details on "Exception 2"

System.Exception: outer ---> System.AggregateException: One or more errors occurred. (Exception 1) (Exception 2) ---> System.Exception: Exception 1
at ExceptionAggregateTest.ExceptionHandlingTests.CollectExceptions() in /home/hannasm/tmp_exc_test/ExceptionHandlingTests.cs:line 16
--- End of inner exception stack trace ---
at ExceptionAggregateTest.ExceptionHandlingTests.CreateAggregate(Exception[] inner) in /home/hannasm/tmp_exc_test/ExceptionHandlingTests.cs:line 29
--- End of inner exception stack trace ---
at ExceptionAggregateTest.ExceptionHandlingTests.CreateOuter(AggregateException inner) in /home/hannasm/tmp_exc_test/ExceptionHandlingTests.cs:line 37

I would like to see the output from Test004 include all the same stack trace details that are included in Test003. (Since there is a slightly different chain of exceptions it will be slightly different output)

This stack overflow post explains the issue pretty well: https://stackoverflow.com/a/34433686/68042

Also, it looks like mstest from my console is doing something separately weird with how it displays aggregate exceptions. Any tips on where to open a ticket about that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtimehelp wanted[up-for-grabs] Good issue for external contributorsquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions