AggregateException2

#region AggregateException2
/*
* 平时用到的都是捕捉单个的异常,而在将任务分解成多个并行的小任务,这时候出现的异常是难以预料的,
* 而 AggregateException 这份类就是用来解决这个问题的 也是用try catch 捕捉,并将异常信息包装成一个实例,
* 然后将这个里面的信息传递到父任务,【根据他报错的先后顺序,和他打印的顺序,嗯?好像发现了什么?倒过来的,
* 嗯,错误纯粹的方式有点像栈的储存,既然用到了栈,那他是的错误信息应该是储存在堆栈上面的
* 因为他是将错误信息包装成一个实例储存的,而实例说白了就是分配内存(虚拟的,碰巧堆栈也是虚拟的
* 而他们的储存和读取方式非常相似(可以看成一个弹匣)】
*
*/

  //public class Example
    //{
    //    public static void Main()
    //    {
    //        //在一个线程里面嵌套一个线程再嵌套一个线程,还是异步的那种
    //        var task1 = Task.Factory.StartNew(() =>//将一个任务分解
    //        {
    //            var child1 = Task.Factory.StartNew(() =>
    //            {
    //                var child2 = Task.Factory.StartNew(() =>
    //                {
    //                    // This exception is nested inside three AggregateExceptions.
    //                    throw new CustomException("Attached child2 faulted.");
    
    //                }, TaskCreationOptions.AttachedToParent);
    //                //TaskCreationOptions 指定控制用于创建和执行任务的可选行为的标志。
    
    //                // This exception is nested inside two AggregateExceptions.
    //                throw new CustomException("Attached child1 faulted.");
    
    //            }, TaskCreationOptions.AttachedToParent);
    //        });
    //        try
        //        {
        //            task1.Wait();
        //        }
        //        catch (AggregateException ae)
        //        {
        //            foreach (var e in ae.Flatten().InnerExceptions) //遍历异常,总的来说  task1 只算
        //            {
        //                if (e is CustomException)
    //                {
    //                    Console.WriteLine(e.Message);
    //                }
    //                else
    //                {
    //                    throw;
    //                }
    //            }
    //        }
    //    }
    //}
    //public class CustomException : Exception
    //{
    //    public CustomException(String message) : base(message)
    //    { }
    //}
    /********************************/
    //    public static void Main()
    //    {
    //       var   task = Task.Run(() =>
    //       { 
    //           var task1 = Task.Factory.StartNew(() =>
    //           {
    //               var task2 = Task.Factory.StartNew(() =>
    //               {
    //                   throw new CustomException("a");
    //               }, TaskCreationOptions.AttachedToParent);
    //               throw new CustomException("a");
    //           }, TaskCreationOptions.AttachedToParent);
    //       });
    //    }
    //    public static void tt()
    //    {
    //        for (int i = 0; i < 10; i++)
    //        {
    //            Console.WriteLine(i);
    //        }
    //    }
    //}

/MSDN 的栗子*/

//using System;
    //using System.Collections.Generic;
    //using System.IO;
    //using System.Threading.Tasks;
    //public class Example
    //{
    //    public static void Main()
    //    {
    //        try
    //        {
    //            ExecuteTasks();
    //        }
    //        catch (AggregateException ae) //异常集合
    //        //https://www.xin3721.com/ArticlePrograme/C_biancheng/3516.html
    //        {
    //            foreach (var e in ae.InnerExceptions) //InnerExceptions 内部错误
    //            {
    //                Console.WriteLine("{0}:\n   {1}", e.GetType().Name, e.Message);// 表示当前实例的确切运行时类型的类型对象。(就是返回错误类型),详细信息
    //            }
    //        }
    //    }

//    static void ExecuteTasks()
//    {
//        String path = @"C:\";//要访问c 盘的节奏喔
//        List<Task> tasks = new List<Task>();//用一个有序列表储存线程,相当于让线程排队

//        tasks.Add(Task.Run(() =>
//        {// 在 tasks 创建一个线程并且运行他
//            // 这应该会抛出一个UnauthorizedAccessException。
//            return Directory.GetFiles(path, "*.txt",//访问指定的路径
//                                      SearchOption.AllDirectories);// AllDirectories  搜索当前目录的所有子目录,嗯?忽然想到了递归
//        }));

//        tasks.Add(Task.Run(() =>
//        {
    //            if (path == @"C:\")
    //                throw new ArgumentException("系统根不是有效路径。");
    //            return new String[] { ".txt", ".dll", ".exe", ".bin", ".dat" };
    //        }));

    //        tasks.Add(Task.Run(() =>
    //        {
    //            throw new NotImplementedException("此操作尚未实现。");
    //        }));

    //        try
    //        {
    //            Task.WaitAll(tasks.ToArray());//等待tasks 
    //        }
    //        catch (AggregateException ae)
    //        {
    //            throw ae.Flatten();
    //            //将异常信息扁平化,说白了就是创建一个实例,将错误信息都封装到这个实例里面,
    //            //然后再由这个实例将异常信息传递到他的父任务?如果时多层嵌套,他是不是一层一层地传呢?
               
    //        }
    //    }
    //}
    // The example displays the following output:
    //       UnauthorizedAccessException:
    //          Access to the path 'C:\Documents and Settings' is denied.
    //       ArgumentException:
    //          The system root is not a valid path.
    //       NotImplementedException:
    //          This operation has not been implemented.
     
    #endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值