ParallelLoopHandleException

CSharp · Console

/Program.cs

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
					
public class Program
{
	public static void Main()
	{
		byte[] data = new byte[5000];
		Random r = new Random();
		r.NextBytes(data);
		
		try
		{
			ProcessDataInParallel(data);
			//ProcessDataInParallelNoExceptionHandling(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 => {
			try 
			{
				if(d < 3) 
					throw new ArgumentException("Value must be greater than 3, Current Value is " + d);
				else
					Console.WriteLine($"d value is {d}");
			}
			catch(Exception e) 
			{
				exceptions.Enqueue(e);
			}
		});
		if(exceptions.Count > 0)
			throw new AggregateException(exceptions);
	}
	
	
	private static void ProcessDataInParallelNoExceptionHandling(byte[] data) 
	{
		Parallel.ForEach(data, d => {
			if(d < 3) 
				throw new ArgumentException("Value must be greater than 3");
			else
				Console.WriteLine($"d value is {d}");
		});
	}
}
;