有多种方法可以观察任务中引发的异常。其中之一在带有 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 后才会观察到?