I have a scenario where a worker thread performs a task that l开发者_如何学运维ooks like
try
{
for()
{
functionA();
functionB();
}
}
catch(Exception ex)
{
throw new Exception("Message");
}
I'm basically re-throwing the exception so that when the worker completes, the UI thread will receive the exception.
Now, if an exception occurs in functionA
I'd like to throw the exception but continue the loop and not stop. How can I achieve this?
edit: If it isn't clear, this is so that the UI can be notified that an exception has occurred but not stop the worker
Something like this:
List<Exception> errorsLog = new List<Exception>();
for()
{
try
{
functionA();
}
catch(Exception e) {
errorsLog.Add(e);
}
try
{
functionB();
}
catch(Exception e) {
errorsLog.Add(e);
}
}
if (errorsLog.Count > 0) {
throw new AggregateException(errorsLog);
}
Where AggregateException
is your custom exception that contains a list of other exceptions.
精彩评论