AggregateException

Exceptions are dealt with on a per-thread basis. However, with an increased emphasis on parallel computing, the situation of having multiple exceptions flowing on different threads becomes a new challenge. When an API spawns parallel tasks and multiple exceptions are thrown from the different executing tasks, you don’t want to be able to analyze all those from the outside, too.

A good example is the new System.Threading parallel constructs introduced in .NET 4, including parallel loop constructs exposed on the Parallel static class:

Parallel.For(0, 10, i => {    if (i % 2 == 0)        throw new Exception("Even number!");    // Do meaningful work.});

The preceding code spawns parallel tasks for each of the loop body executions, ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.