首页 > 其他 > 详细

抛出异常不会终止程序:

时间:2014-12-02 20:32:24      阅读:198      评论:0      收藏:0      [点我收藏+]

以下为测试代码:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestPDAService
{
    [TestClass]
    public class UnitTest2
    {
        [TestMethod]
        public void TestMethod1()
        {
            string error = "";
            string str=string.Empty;
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    if (i / 2 == 0)
                    {
                        bool flag = str.Contains("test");
                    }
                    else
                    {
                        throw new Exception(string.Format("{0}抛出异常", i));
                    }
                }
                catch (Exception ex) 
                {
                    error += ex.Message;
                }          
            }
            Console.Write(error);
            Console.Write("执行到此");

            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }
    }
}

程序捕获异常后会继续执行异常之后的程序!

抛出异常不会终止程序:

原文:http://www.cnblogs.com/Unrmk-LingXing/p/4138282.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!