msdn中针对并行循环中异常的处理给出了示例,如下。(个人觉得还不错)
在此示例中,将捕获所有异常,然后将它们包装在所引发的 System.AggregateException 中。调用方可以决定要处理哪些异常。
class ExceptionDemo2
{
static void Main(string[] args)
{
// Create some random data to process in parallel.
// There is a good probability this data will cause some exceptions to be thrown.
byte[] data = new byte[5000];
Random r = new Random();
r.NextBytes(data);
try
{
ProcessDataInParallel(data);
}
catch (AggregateException ae)
{
// This is where you can choose which exceptions to handle.
foreach (var ex in ae.InnerExceptions)
{
if (ex is ArgumentException)
Console.WriteLine(ex.Message);
else
throw ex;
}
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
private static void ProcessDataInParallel(byte[] data)
{
// Use ConcurrentQueue to enable safe enqueueing from multiple threads.
var exceptions = new ConcurrentQueue<Exception>();
// Execute the complete loop and capture all exceptions.
Parallel.ForEach(data, d =>
{
try
{
// Cause a few exceptions, but not too many.
if (d < 0x3)
throw new ArgumentException(String.Format("value is {0:x}. Elements must be greater than 0x3.", d));
else
Console.Write(d + " ");
}
// Store the exception and continue with the loop.
catch (Exception e) { exceptions.Enqueue(e); }
});
// Throw the exceptions here after the loop completes.
if (exceptions.Count > 0) throw new AggregateException(exceptions);
}
}