C# 异步编程Task整理(二)异常捕捉

时间:2021-08-13 01:43:01

一、在任务并行库中,如果对任务运行Wait、WaitAny、WaitAll等方法,或者求Result属性,都能捕获到AggregateException异常。

可以将AggregateException异常看做是任务并行库编程中最上层的异常。

在任务中捕获的异常,最终都应该包装到AggregateException中。一个任务并行库异常的简单处理示例如下:

static void TestTwo() { Task t1= Task.Factory.StartNew(() => { throw new Exception("执行失败"); }); try { //主线程等待,可以 捕捉异常 t1.Wait(); } catch (AggregateException ex) { foreach (var item in ex.InnerExceptions) { Console.WriteLine("异常类型:{0}{1}来自: {2} {3} 异常内容:{4} ", item.GetType(), Environment.NewLine, item.Source, Environment.NewLine, item.Message); } Console.Write(ex.Message); } }

二、方式2使用主线程委托,这种方式比较推荐

在这个例子中,我们声明了一个委托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; } }

三、TaskScheduler.UnobservedTaskException

注意 任务调度器TaskScheduler提供了这样一个功能,它有一个静态事件用于处理未捕获到的异常。一般不建议这样使用,因为事件回调是在进行垃圾回收的时候才发生的。

static void TestFourth() { TaskScheduler.UnobservedTaskException += new EventHandler<UnobservedTaskExceptionEventArgs>(TaskScheduler_UnobservedTaskException); Task t1 = new Task(() => { throw new Exception("任务并行编码中 产生 未知错误"); }); t1.Start(); Console.ReadKey();//主线程等待 t1 = null; //GC.Collect(0); //只有在回收t1对象 的时候才会触发UnobservedTaskException Console.WriteLine("主线程马上结束"); Console.Read(); } static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) { foreach (Exception item in e.Exception.InnerExceptions) { Console.WriteLine("异常类型:{0}{1}来自:{2}{3}异常内容:{4}", item.GetType(), Environment.NewLine, item.Source, Environment.NewLine, item.Message); } //将异常标识为已经观察到 e.SetObserved(); }