抛出异常不会终止程序:

以下为测试代码:

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

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

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