Monday, December 26, 2016

How to handle exception in Parallel.For and Parallel.Foreach loop? – Task Parallel Library


In my previous article, I explained about Parallel.For andParallel.Foreach loop in detail. In this article I will explain, how to handle exception in Parallel.For and Parallel.Foreach loop.

Parallel.For and Parallel.Foreach methods doesn’t provide any special mechanism to handle exception thrown from parallel loops. We can catch unhandled exceptions using try catch block inside parallel loop. When you add any try catch block to catch exception in parallel loop, there is possibility that the same exception may throw by multiple threads in parallel. So we need to wrap try catch block inside parallel loop and also on method from where we call parallel loop to catch all types of exceptions. See below example.

Code –

Output –


As you can see in above example, all exception thrown from Parallel.For loop is captured as ArgumentException and added to ConcurrentQueue from multiple threads. If parallel loop has any exception occurred, then we need to iterate all the exception to get exception message.

References –

See also –

No comments:

Post a Comment