6
foreach (Foo el in arr) {
// ...
Validate(el);
// ...
}
In the example code, when the foreach
is executed, an exception can be triggered from the function Validate
which will be treated on who called the method containing the loop.
The exceptions triggered by Validate
has to be handled by the user. The most convenient would be for the user to know all the errors that were played to correct them before re-running the function that contains the loop.
For that, I’d 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 I fire multiple exceptions?
I’m not sure I understand the explanation of the problem. The conclusion of what I would have to do to solve it and perhaps that is why the question itself does not seem to make sense and the answer would be "no". Maybe because you don’t understand the problem you can’t see the motivation to try this.
– Maniero
Should exceptions be used for flow control? (https://answall.com/a/48458/41056)
– vinibrsl