有多种方法可以观察任务中抛出的异常。其中一个是在带有OnlyOnFaulted的ContinueWith中:
var task = Task.Factory.StartNew(() =>
{
// Throws an exception
// (possibly from within another task spawned from within this task)
});
var failureTask = task.ContinueWith((t) =>
{
// Flatten and loop (since there could have been multiple tasks)
foreach (var ex in t.Exception.Flatten().InnerExceptions)
Console.WriteLine(ex.Message);
}, TaskContinuationOptions.OnlyOnFaulted);
我的问题:一旦failureTask开始,是否会自动观察异常,或者只有在我触摸'ex.Message时才会被观察到异常?