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
  • 105,294
  • 20
  • 131
  • 182

2 Answers2

5

You're looking for the AggregateException class.

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
2

Just bundle your list into a super-exception:

class MultiException : Exception
{
    public List<Exception> ExceptionsList { get; set; }
}
AVIDeveloper
  • 2,594
  • 1
  • 23
  • 28