Task中专门的异常处理:AggregateException
//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); } }
使用主线程委托,这种方式比较推荐 在这个例子中,我们声明了一个委托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; } }