using System.Threading.Tasks;
using System.Collections.Concurrent;
public static void Main()
byte[] data = new byte[5000];
ProcessDataInParallel(data);
catch(AggregateException ae)
foreach(var e in ae.Flatten().InnerExceptions)
Console.WriteLine("Exception:" + e.Message);
private static void ProcessDataInParallel(byte[] data)
var exceptions = new ConcurrentQueue<Exception>();
Parallel.ForEach(data, d => {
throw new ArgumentException("Value must be greater than 3, Current Value is " + d);
Console.WriteLine($"d value is {d}");
throw new AggregateException(exceptions);
private static void ProcessDataInParallelNoExceptionHandling(byte[] data)
Parallel.ForEach(data, d => {
throw new ArgumentException("Value must be greater than 3");
Console.WriteLine($"d value is {d}");