Handling exceptions in a PLINQ query
This recipe will describe how to handle exceptions in a PLINQ query.
Getting ready
To work through this recipe, you will need Visual Studio 2015. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter7\Recipe4
.
How to do it...
To understand how to handle exceptions in a PLINQ query, perform the following steps:
Start Visual Studio 2015. 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; using static System.Console;
Add the following code snippet inside the
Main
method:IEnumerable<int> numbers = Enumerable.Range(-5, 10); var query = from number in numbers select 100 / number; try { foreach(var n in query) WriteLine(n); } catch (DivideByZeroException) { WriteLine("Divided by zero!"); } WriteLine("---"); WriteLine("Sequential LINQ query processing"); WriteLine(); var parallelQuery...