Suppose I have an array of tasks say taskArray
and I create a task continuation using ContinueWhenAll
and one or more of the tasks in taskArray
throw some exception. My question is, is there any scenario where this may lead to an UnobservedTaskException
?
So basically the question boils down to, does ContinueWhenAll
observe the exceptions in taskArray
like Wait
would do for a single task? If no, then what should be used for a group of tasks if I don't want to look explicitly at the exceptions on each task. I don't want to use WaitAll
as it is not available for generic tasks.
davenewza's answer suffices if catching the exceptions from tasks application wide is acceptable.
If not then you have to do what you don't want to do (observe the exception in some way). You have two options:
OnlyOnFaulted
case whose only job is to observe the exception by looking at the Exception
property on the task.In your continuation for ContinueWhenAll, you can split the tasks into those with exceptions and those without:
Task.Factory.ContinueWhenAll(tasks, ts =>
{
var lookup = ts.ToLookup(t => t.Exception != null);
var faultedTasks = lookup[true];
var nonFaultedTasks = lookup[false];
});