异常处理
using System; using System.Text; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace Test { class Program { /// <summary> /// 异常处理 /// </summary> static void Main() { var intArray = new int[] { 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8 }; var result = intArray.AsParallel().Select(i => 1000 / i); //抛出一个预期的被零除异常 try { result.ForAll(item => { Console.WriteLine(item); }); } catch (AggregateException ae) { Console.WriteLine("\n异常信息:"); foreach (var item in ae.InnerExceptions) { Console.WriteLine(item.Message); } } Console.WriteLine("\n执行完成"); Console.ReadLine(); } } }
打印运行结果:
/* 200 142 111 异常信息: 尝试除以零。 尝试除以零。 执行完成 */

浙公网安备 33010602011771号