foreach (Foo el in arr) {
// ...
Validate(el);
// ...
}
In the example code, when foreach
is executed, an exception can be raised from the Validate
function that will be handled in who called the method that contains the loop.
Exceptions triggered by Validate
have to be handled by the user. The most convenient would be for the user to know all the errors that were thrown to correct them before re-executing the function that contains the loop.
For this, I would have to do something like:
List<Exception> errors = new List<Exception>();
foreach (Foo el in arr) {
// ...
try {
Validate(el);
} catch(Exception e) {
errors.Add(e);
}
// ...
}
if(erros.Any()) {
throw errors; // somente objetos do tipo Exception podem ser disparados com throw.
}
How do I trigger multiple exceptions?