Handling exceptions in a PLINQ query
This recipe will describe how to handle exceptions in a PLINQ query.
Getting ready
To step through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found in BookSamples\Chapter7\Recipe4
.
How to do it...
To understand how to handle exceptions in a PLINQ query, perform the following steps:
Start Visual Studio 2012. Create a new C# Console Application project.
In the
Program.cs
file add the followingusing
directives:using System; using System.Collections.Generic; using System.Linq;
Add the following code snippet inside the
Main
method:IEnumerable<int> numbers = Enumerable.Range(-5, 10); var query = from number in numbersselect 100 / number; try { foreach(var n in query) Console.WriteLine(n); } catch (DivideByZeroException) { Console.WriteLine("Divided by zero!"); } Console.WriteLine("---"); Console.WriteLine("Sequential LINQ query processing"); Console.WriteLine(); var parallelQuery...