C# › Exception Handling
How does AggregateException work with Task.WhenAll() when multiple concurrent tasks throw exceptions?
AdvancedAnswer
Task.WhenAll() wraps ALL exceptions from ALL failed tasks into a SINGLE AggregateException, accessible via its .InnerExceptions collection; however, when you 'await' the WhenAll() result directly, only the FIRST exception is actually re-thrown to you (as its own specific type) — you must inspect the ORIGINAL Task objects individually if you need every failure's details.
var task1 = Task.Run(() => throw new InvalidOperationException("Error 1"));
var task2 = Task.Run(() => throw new ArgumentException("Error 2"));
try {
await Task.WhenAll(task1, task2); // only the FIRST exception surfaces here
} catch (Exception ex) {
// To see ALL errors, check task1.Exception and task2.Exception individually
}Real-world example Collecting and reporting EVERY failure from a batch of concurrent operations, not just the first one encountered.