怎样使用c#模拟一个异常使程序崩溃,还能弹出“错误

2025-05-10 05:29:16
推荐回答(1个)
回答1:

就是人为抛出异常吧?可以这么做:

static void Main(string[] args)
        {
            string a = "abc";
            int b = Convert.ToInt32(a);//字符串转int失败,抛出异常
            Console.WriteLine(b);
            Console.ReadKey();

        }

在控制台程序中按F5运行程序,就会抛出异常,如下图所示: