码迷,mamicode.com
首页 > 其他好文 > 详细

抛出异常不会终止程序:

时间:2014-12-02 20:33:18      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   sp   for   on   div   

以下为测试代码:

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();
        }
    }
}

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

抛出异常不会终止程序:

标签:style   blog   io   color   os   sp   for   on   div   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!