Task中专门的异常处理:AggregateException

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//AggregateException:是一个异常集合,因为Task中可能抛出异常,所以我们需要新的类型来收集异常对象
static void Method11()
{
    var task = Task.Factory.StartNew(() =>
    {
        var childTask1 = Task.Factory.StartNew(() =>
        {
            //实际开发中这个地方写你处理的业务,可能会发生异常....
  
            //自己模拟一个异常
            throw new Exception("my god!Exception from childTask1 happend!");
        }, TaskCreationOptions.AttachedToParent);
  
        var childTask2 = Task.Factory.StartNew(() =>
        {
            throw new Exception("my god!Exception from childTask2 happend!");
        }, TaskCreationOptions.AttachedToParent);
    });
    try
    {
        try
        {
            task.Wait();   //1.异常抛出的时机
        }
        catch (AggregateException ex)  //2.异常所在位置
        {
            foreach (var item in ex.InnerExceptions)
            {
                Console.WriteLine(item.InnerException.Message + "     " + item.GetType().Name);
            }
  
            //3.异常集合,如果你想往上抛,需要使用Handle方法处理一下
            ex.Handle(p =>
            {
                if (p.InnerException.Message == "my god!Exception from childTask1 happend!")
                    return true;
                else
                    return false; //返回false表示往上继续抛出异常
            });
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("-----------------------------------------------------");
        Console.WriteLine(ex.InnerException.InnerException.Message);
    }
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
使用主线程委托,这种方式比较推荐
 
 在这个例子中,我们声明了一个委托AggregateExceptionCatchHandler,它接受两个参数,一个是事件的通知者;另一个是事件变量AggregateExceptionArgs。AggregateExceptionArgs是为了包装异常而新建的一个类型。在主线程中,我们为事件AggregateExceptionCatched分配了事件处理方法Program_AggregateExceptionCatched,当任务Task捕获到异常时,代码引发事件。
 
这种方式完全没有阻滞主线程。如果是在Winform或WPF窗体程序中,要在事件处理方法中处理UI界面,还可以将异常信息交给窗体的线程模型去处理。所以,最终建议大家采用事件通知的模型处理Task中的异常。
 
//定义AggregateException 的事件委托
static event EventHandler<AggregateExceptionArgs> AggregateExceptionCatched;
static void TestThree()
{
    AggregateExceptionCatched += new EventHandler<AggregateExceptionArgs>(Program_AggregateExceptionCatched);
 
    Task t1 = new Task(() =>
    {
        try
        {
            throw new InvalidOperationException("任务并行编码 中产生未知错误");
        }
        catch (Exception ex)
        {
            AggregateExceptionArgs args = new AggregateExceptionArgs()
            {
                AggregateException = new AggregateException(ex)
            };
            //使用主线程委托代理,处理子线程 异常
            //这种方式没有阻塞 主线程或其他线程
            AggregateExceptionCatched?.Invoke(null, args);
        }
    });
    t1.Start();
}
static void Program_AggregateExceptionCatched(object sender, AggregateExceptionArgs e)
{
    foreach (var item in e.AggregateException.InnerExceptions)
    {
        Console.WriteLine("异常类型:{0}{1}来自:{2}{3}异常内容:{4}",
            item.GetType(), Environment.NewLine, item.Source,
            Environment.NewLine, item.Message);
    }
}
//定义异常参数处理
public class AggregateExceptionArgs : EventArgs
{
    public AggregateException AggregateException { get; set; }
}

  

 

posted on   itjeff  阅读(84)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-11-22 js数组遍历方法总结
2019-11-22 Web.Config配置文件中customErrors元素的使用方法
2016-11-22 Layout Renderers
2016-11-22 Nlog 配置总结

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示