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).

4
  • 1
    See this highly upvoted answer: stackoverflow.com/a/178464/555547 Commented Feb 24, 2012 at 21:14
  • @Jason that's great but that method of rethrow only works from inside a catch block. I'm outside of it. Commented Feb 24, 2012 at 21:16
  • 1
    @Jason: He wants to throw multiple exceptions. That won't work. Commented Feb 24, 2012 at 21:16
  • Just noticed that, see stackoverflow.com/a/278543/555547 Commented Feb 24, 2012 at 21:16

2 Answers 2

5

You're looking for the AggregateException class.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I need .net 4, but I see I can make my own. Thanks.
2

Just bundle your list into a super-exception:

class MultiException : Exception { public List<Exception> ExceptionsList { get; set; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.