Task、AggregateException相关的异常处理
创建一个本身抛出异常、子任务抛出异常、子任务的子任务抛出异常的Task。
Task pt = new Task(() =>
{
Task.Factory
.StartNew(() =>
{
throw new Exception("ex 1");
}, TaskCreationOptions.AttachedToParent);
Task.Factory
.StartNew(() =>
{
Task.Factory
.StartNew(() =>
{
throw new Exception("ex 2-1");
}, TaskCreationOptions.AttachedToParent);
throw new Exception("ex 2");
}, TaskCreationOptions.AttachedToParent);
throw new Exception("ex 3");
});
pt.Start()开始任务,异常不会抛出,但必须被处理,以下是若干种方法。
//方法1:
pt.ContinueWith(t =>
{
t.Exception.Handle(ex =>
{
Console.WriteLine(ex.Message);
return true;
});
}, TaskContinuationOptions.OnlyOnFaulted);
//方法2:
pt.ContinueWith(t =>
{
t.Exception.Handle(ex =>
{
Console.WriteLine(ex.GetBaseException().Message);
return true;
});
}, TaskContinuationOptions.OnlyOnFaulted);
//方法3:
pt.ContinueWith(t =>
{
foreach (var ex in t.Exception.Flatten().InnerExceptions)
{
Console.WriteLine(ex.Message);
}
}, TaskContinuationOptions.OnlyOnFaulted);
//方法4:
pt.ContinueWith(t =>
{
foreach (var ex in t.Exception.InnerExceptions)
{
Console.WriteLine(ex.Message);
}
}, TaskContinuationOptions.OnlyOnFaulted);
上述方法中只有方法3可以获取到每个异常根源。由于AggregateException是一个"包含异常的集合",故多层抛出异常的子任务可能产生"异常嵌套",方法1简单易用,但无法遍历子任务中异常集合;方法2中GetBaseException在子任务嵌套下同样获取不到异常根源;方法4与方法2类似;方法3Flatten()方法是对异常的"平展"操作,将真正的异常先写入了集合进行再遍历。
实际使用中,简单任务推荐使用方法1,多层子任务推荐使用方法3。

浙公网安备 33010602011771号