CodeBlanks Logo Icon CodeBlanks

AggregateException

C# course · Advanced · Lesson 14/83

Learn multi-task errors with AggregateException.

AggregateException

AggregateException bundles multiple errors under a single exception. It appears especially in Task.WhenAll scenarios where multiple tasks run in parallel; multiple tasks may fault at once.

Each inner error is listed in the InnerExceptions collection. Example: catch (AggregateException aggr) { foreach (var inner in aggr.InnerExceptions) { ... } }. This lets you inspect each error one by one.

InnerExceptions

AggregateException.InnerExceptions is a ReadOnlyCollection that holds all the inner exceptions. You iterate to reach each one. This is especially useful in WhenAll to find which tasks faulted.

If you only care about a single error, aggr.InnerException gives the first. The Flatten() method unwraps nested AggregateExceptions; this is useful in deep chains where child tasks also throw AggregateExceptions.

Exercise Inspect AggregateException

Define two async methods: static async Task FailA() and static async Task FailB(). In each body write await Task.CompletedTask; and throw new InvalidOperationException("x");. Make Main a static async Task Main. Run them in parallel with Task all = Task.WhenAll(FailA(), FailB());. Print the AggregateException name with try { await all; } catch (Exception) { Console.WriteLine(all.Exception.GetType().Name); }.

Show example solution
In FailA and FailB body write await Task.CompletedTask; and throw new InvalidOperationException("x");. Make Main static async Task Main() and write Task all = Task.WhenAll(FailA(), FailB()); try { await all; } catch (Exception) { Console.WriteLine(all.Exception.GetType().Name); }
Example solution:
using System;
using System.Threading.Tasks;

class Program
{
    static async Task FailA()
    {
        await Task.CompletedTask;
        throw new InvalidOperationException("x");
    }

    static async Task FailB()
    {
        await Task.CompletedTask;
        throw new InvalidOperationException("x");
    }

    static async Task Main()
    {
        Task all = Task.WhenAll(FailA(), FailB());
        try
        {
            await all;
        }
        catch (Exception)
        {
            Console.WriteLine(all.Exception.GetType().Name);
        }
    }
}

Test yourself

  1. Complete the catch type

    try { await Task.WhenAll(t1, t2); } catch (____ ex) { foreach (var inner in ex.InnerExceptions) { Console.WriteLine(inner.Message); } }
    Show answer
    try { await Task.WhenAll(t1, t2); } catch (AggregateException ex) { foreach (var inner in ex.InnerExceptions) { Console.WriteLine(inner.Message); } }
  2. Match AggregateException concepts

    AggregateException
    Bundles many errors
    InnerExceptions
    Inner errors collection
    Flatten()
    Unwraps nesting
    InnerException
    First inner error
  3. When does AggregateException arise?

    • Only when a single error is thrown
    • In async void methods
    • When doing CPU work with Task.Run
    • When multiple tasks fault in parallel (e.g. WhenAll)
    Show answer

    When multiple tasks fault in parallel (e.g. WhenAll) — Correct. AggregateException arises especially in parallel scenarios like WhenAll.

All lessons in Advanced