AggregateException.Flatten Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Flattens an AggregateException instances into a single, new instance.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

member Flatten : unit -> AggregateException

Return Value

Type: System.AggregateException

A new, flattened AggregateException.

If a task has an attached child task that throws an exception, that exception is wrapped in an AggregateException exception before it is propagated to the parent task, which wraps that exception in its own AggregateException exception before it propagates it back to the calling thread. In such cases, the InnerExceptions property of the AggregateException exception that is caught by the Task.Wait, Task<'TResult>.Wait, Task.WaitAny or Task.WaitAll method contains one or more AggregateException instances, not the original exceptions that caused the fault. To avoid having to iterate over nested AggregateException exceptions, you can use the Flatten method to remove all the nested AggregateException exceptions, so that the InnerExceptions property of the returned AggregateException object contains the original exceptions.

This method recursively flattens all instances of AggregateException exceptions that are inner exceptions of the current AggregateException instance . The inner exceptions returned in the new AggregateException are the union of all the inner exceptions from exception tree rooted at the current AggregateException instance.

In the following example, nested AggregateException instances are flattened and handled in just one loop.

No code example is currently available or this language may not be supported.

You can also use the AggregateException.Flatten method to rethrow the inner exceptions from multiple AggregateException instances thrown by multiple tasks in a single AggregateException instance, as the following example shows.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top