You can use AggregateException
for do what you want:
try
{
List<Exception> excepcoes = new List<Exception>();
excepcoes.Add(new ArgumentException("argumento", "argumento invalido"));
excepcoes.Add(new ArgumentNullException("argumento nulo"));
excepcoes.Add(new InvalidCastException("operacao invalida"));
throw new AggregateException(excepcoes);
}
catch(AggregateException aEx)
{
foreach(var ex in aEx.InnerExceptions)
{
Console.WriteLine("{0}: {1}\n", ex.GetType(), ex.Message);
// Output:
// System.ArgumentException: argumento
// Parameter name: argumento invalido
// System.ArgumentNullException: Value cannot be null.
// Parameter name: argumento nulo
// System.InvalidCastException: operacao invalida
}
}
The AggregateException
constructor accepts a list of exceptions, which can then be accessed through the .InnerExceptions
property.
See here an example in dotFiddle.