O'Reilly logo

C# Multithreaded and Parallel Programming by Rodney Ringler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling exceptions in parallel loops

Well, no matter how hard we try to write error-free code, the real world intervenes and inevitably there are exceptions that our code will encounter, for example, file not found, argument out or range, and so on. When we are processing a parallel loop command, since all of the iterations are running on potentially different threads, we need a way to gather all of the exceptions that any of the iterations of the loop may produce.

.NET provides the AggregateException class for just this purpose. This allows us to collect all of the exceptions into the AggregateException object and then "catch" it once the loop has completed.

Think of this like you would a normal error-handling situation. Typically, you put a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required