异常处理

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

异常信息:
尝试除以零。
尝试除以零。

执行完成
 
*/

 

posted @ 2016-09-06 12:39  茗::流  阅读(80)  评论(0)    收藏  举报
如有雷同,纯属参考。如有侵犯你的版权,请联系我。