#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