Aggregate exceptions multiple exceptions from one method

Who says you cannot throw multiple exceptions in one method. If you are not used to playing around with AggregateExceptions you may be tempted to create your own data-structure to represent many things going wrong. There are of course were another data-structure that is not an exception would be more ideal such as the results of a validation. Even if you do play with AggregateExceptions you may be on the receiving side and always handling them not realizing they can be of use to you.

It is quite plausible to have a method execute and even though it will be a failure as a whole you will want to highlight multiple things that went wrong in the exceptions that are thrown. As an example this behavior can be seen with how Parallel methods work were a task broken into multiple threads and any number of them could throw exceptions and this needs to be reported. Here is a silly example of how you could benefit from this:

publicvoidRun(){try{this.SillyMethod(1,2);}catch(AggregateExceptionex){Console.WriteLine(ex.Message);foreach(ExceptioninnerExceptioninex.InnerExceptions){Console.WriteLine(innerException.Message);}}}privatevoidSillyMethod(intinput1,intinput2){varexceptions=newList<Exception>();if(input1==1){exceptions.Add(newArgumentException("I do not like ones"));}if(input2==2){exceptions.Add(newArgumentException("I do not like twos"));}if(exceptions.Any()){thrownewAggregateException("Funny stuff happended during execution",exceptions);}}