Handling parallel exceptions in dotnet

I’m executing several tasks in parallel, where more than one throw exceptions. I want to handle all the exceptions, which basically means catching them all and being able to, at least, rethrowing them with all the info they carry.

So, here it is how

var runnersTasks = new List<Task>();// list to keep the tasks created
Task compositeTask = null; // task consisting in waiting for all the tasks to finish in parallel
    // run all the tasks and keep them in the list
    Runners.ForEach(r => runnersTasks.Add(r.RunAsync()));

    // just wait for them all to finish, and keep the reference
    compositeTask = Task.WhenAll(runnersTasks);
    await compositeTask;
catch (Exception) // here if you actually catch the exception, you'll find it's only the first one thrown
    //usually just throw back the full AggregateException, rather than just the first exception catched here
    throw compositeTask.Exception;

The cathed exception is only the first one thrown.

That’s right. The list of all the exceptions is actually in the AggregateException kept in the property Exception of the compositeTask.

So, yes, the handling is that ugly.

Talk is cheap

Here’s the code.