Just like other parallel primitives, PLINQ throws a System.AggregateException whenever it encounters an exception. Exception handling largely depends on your design. You may want the program to fail as soon as possible or you may want all the exceptions to be returned to the caller.
In the following example, we will wrap a parallel query inside a try-catch block. When the query throws an exception, it will propagate back to the caller, wrapped in System.AggregateException:
var range = ParallelEnumerable.Range(1, 20);
ParallelQuery<int> query= range.Select(i => i / (i - 10)).WithDegreeOfParallelism(2);
try
{
query.ForAll(i => Console.WriteLine(i));
}
catch (AggregateException aggregateException)
{
foreach (var ex in aggregateException.InnerExceptions)
{
Console.WriteLine(ex.Message);
if (ex is DivideByZeroException...