2
public void DoRiskyThings(List<Action> tasks)
{
  List<Exception> exceptions = new List<Exception>();
  foreach(Action task in tasks)
  {
    try {task()}
    catch (Exception e) {exceptions.Add(e)};
  }

  if (exceptions.Any())
  {
    //... what goes here?
  }
}

I'd like to preserve all the information (especially messages and stacktraces).

Amy B
  • 108,202
  • 21
  • 135
  • 185

2 Answers2

5

You're looking for the AggregateException class.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Just bundle your list into a super-exception:

class MultiException : Exception
{
    public List<Exception> ExceptionsList { get; set; }
}
AVIDeveloper
  • 2,954
  • 1
  • 25
  • 32