Created
October 30, 2018 05:21
-
-
Save ssippe/77b8397d7b398225d8d6c0aa668dfbeb to your computer and use it in GitHub Desktop.
AggregateException Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ProcessList(List<Input> inputs) | |
{ | |
var errors = new List<Exception>(); | |
foreach (var input in inputs) | |
{ | |
try | |
{ | |
Process(input); | |
} | |
catch (Exception ex) | |
{ | |
errors.Add(new Exception($"error processing {input}", ex)); | |
//Continue processing the rest of the list and throw the | |
//any exceptions at the end. | |
} | |
} | |
if (errors.Any()) | |
{ | |
throw new AggregateException($"{errors.Count} of {inputs.Count} failed", errors); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment