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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.