我有一些父/子任务的层次结构:
var t1 = Task.Factory.StartNew(() =>
{
var t21 = Task.Factory.StartNew(() =>
{
throw new Exception("Inner fault");
}, TaskCreationOptions.AttachedToParent);
var t22 = t21.ContinueWith(ant =>
{
Console.WriteLine("Inner handler: " + ant.Exception.InnerException.Message);
}, TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.OnlyOnFaulted);
throw new Exception("Outer fault");
});
try
{
t1.Wait();
}
catch (AggregateException ae)
{
foreach (var e in ae.Flatten().InnerExceptions)
Console.WriteLine("Outer handler: " + e.Message);
}
结果,“外部处理程序”处理已由“内部处理程序”处理的异常:
Inner handler: Inner fault
Outer handler: Outer fault
Outer handler: Inner fault
是否可以防止在“外部句柄”(我的示例中为“内部处理程序”)中处理已处理的异常?