Information Technology Reference
In-Depth Information
try
{
foreach ( var item in nums)
Console .WriteLine(item);
}
catch ( InvalidOperationException inv)
{
// elided
}
Once PLINQ is involved, you must enclose the definition of the query in
the try / catch block as well. And, of course, remember that once you use
PLINQ, you must catch AggregateException instead of whatever excep-
tion you had been originally expecting. This is true whether you use
Pipelining, Stop and Go, or Inverted Enumeration.
Exceptions are complicated in any algorithm. Parallel tasks create more
complications. The Parallel Task Library uses the AggregateException class
to hold any and all exceptions thrown somewhere in the depths of your
parallel algorithms. Once any of the background threads throws an excep-
tion, any other background operations are also stopped. Your best plan is to
try to ensure that no exceptions can be thrown from the code executing in
your parallel tasks. Even so, other exceptions that you don't expect may be
thrown elsewhere. That means you must handle any AggregateException in
the controlling thread that initiated all the background work.
 
Search WWH ::




Custom Search