Consider the following snippet of code:
foreach (var setting in RequiredSettings)
{
try
{
if (!BankSettings.Contains(setting))
{
throw new Exception("Setting " + setting + " is required.");
}
}
catch (Exception e)
{
catExceptions.Add(e);
}
}
}
if (catExceptions.Any())
{
throw new AggregateException(catExceptions);
}
}
catch (Exception e)
{
BankSettingExceptions.Add(e);
}
if (BankSettingExceptions.Any())
{
throw new AggregateException(BankSettingExceptions);
}
catExceptions is a list of exceptions that I add to. When the loop is done I then take that list and add them to the AggregateException then throw it. When I run the debugger each of the string messages ‘Setting X is required’ appears in the catExceptions collection. However, when it comes down to the AggregateException the only message is now ‘One or more errors have occurred’.
Is there a way that I can aggregate while still keeping the individual messages?
Thanks!
Yes. The InnerExceptions property will include all of the exceptions, with their messages.
You could display these as needed. For example: