zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    IIS安装和使用(Windows Server 2003)
    五险一金
    CKEditor与CKFinder的配置(ASP.NET环境)
    spring的@ConditionalOnMissingBean注解
    spring的@Primary注解
    java8实现接口需要重写接口中的default方法吗
    消息队列的作用
    ES快速入门,ElasticSearch 搜索引擎
    spring的@PostConstruct 和 @PreDestroy 注解
    Spring注解@Component、@Repository、@Service、@Controller区别
  • 原文地址:https://www.cnblogs.com/Jusfr/p/2523795.html
Copyright © 2011-2022 走看看