c# - Handling multiple exceptions in a loop -


in code, method being called repeatedly within loop so:

foreach (var file in files) {     somemethod(file); } 

the method throw exceptions, don't want code exit loop after first exception.

furthermore, code above being called web api controller, need way pass exception information controller, handled (log exception , return error response client).

what i've done far catch , store exception in list.

var errors = new list<exception>();  foreach (var file in files) {     try     {         somemethod(file);     }     catch(exception ex)     {         errors.add(ex);     } } 

considering rethrowing errors in list not option, best approach return exception information controller?

use aggregateexception.

you can pass list<exception> constructor , throw that.

at end of loop do:

aggregateexception aggregateex = new aggregateexception(errors); throw aggregateex; 

(or return aggregateexception)


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -