How should I handle Errors on a list of Actions that each can throw an error/exception?
I am trying to figure out how and where I should handle my errors from a list of actions that I have to carry out:
Summary:
I am passing a List and a Settings object to a ProcessActions class. The ProcessActions class iterates through the list of strings and gets the appropriate instantiated object from my factory and calls the object.Execute() on each object.
class ProcessActions : IEngine
{
public ActionFactory Factory { get; set; }
public IList<string> Actions { get; set; }
public ISettings Settings { get; set; }
public ProcessActions(IList<string> actions, ISettings settings)
{
Factory = new ActionFactory();
Actions = actions;
Settings = settings;
}
public void Execute()
{
foreach (var action in Actions)
{
var task = Factory.CreateInstance(action, Settings);
task.Execute();
}
}
}
开发者_运维知识库Originally I was recording any errors in a dictionary that I pass back to the object that called .Execute(). However, would I be able to gather any thrown exceptions in each object and return those so that I could log them in this ProcessAction (some type of collection)?
.NET 4 introduced the AggregateException which is essentially what you're asking. It appears as though it was introduced specifically for the PLINQ and other parallel library related objects which could have multiple threads all throwing exceptions. It's a throwable object just like any other exception.
It looks like you're not executing in parallel so this isn't quite what it was designed for but maybe it can be used or the concept adapted. See the remarks section of the referenced page.
精彩评论