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 want to be able to analyze all those when the parallel operation completes.

A good example is the System.Threading parallel constructs introduced in .NET 4.0, 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 ...

Get C# 5.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.