liveBook · Manning

AggregateException

As explained in C# Concurrency: Asynchronous and multithreaded programming

AggregateException is a .NET exception type that represents one or more exceptions produced by an operation or group of operations.

AggregateException wraps one or more exceptions produced by a faulted Task. Task.Exception, Wait, and Result expose it when a task fails, with original failures stored in InnerExceptions rather than InnerException. section 3.2.4 section 12.2

Handle the exception according to how the task is observed:

  • Inspect task.Exception after checking IsFaulted when you need the aggregate without immediately throwing. section 3.2.4
  • Use InnerExceptions to reach the contained failures; do not expect the singular InnerException property to hold them. section 3.2.4
  • Expect await to throw the first contained exception instead of AggregateException, even when multiple exceptions are stored. section 12.2

A task with a stored failure

TaskCompletionSource can store a NotSupportedException in a faulted task. Reading Task.Exception reveals AggregateException; awaiting the same task throws NotSupportedException. section 12.2

var tcs = new TaskCompletionSource();
tcs.SetException(new NotSupportedException());
Console.WriteLine("In tasks: "+tcs.Task.Exception.GetType());  #1
try
{
   await tcs.Task;                                             #2
}
catch(Exception ex)
{
   Console.WriteLine("Thrown: "+ex.GetType());                 #3
}

Compare the exception type stored by a task with the exception type propagated by await. section 12.2

How C# Concurrency: Asynchronous and multithreaded programming covers it

How deep it goes
Distinguishes Task.Exception, InnerExceptions, Wait, Result, and await propagation, but does not detail exception-handling policy.
Examples
Uses faulted TaskCompletionSource code and IsFaulted, Wait, and InnerExceptions inspection patterns.
What it assumes
Assumes familiarity with Task completion and faulted states while clarifying aggregate storage and await behavior.
What you can do after
Diagnose task failures by inspecting aggregates or catching the original exception propagated by await.
How it connects
Connects Task, async/await, Task.WhenAll, TaskCompletionSource, and exception handling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C# Concurrency: Asynchronous and multithreaded programming. The section links point to the parts of the book it draws on.