可以根据需求定制异常并在适当的地方抛出
class 异常Ex:Exception { private string testsss; public string Testsss { get=> testsss; } public 异常Ex(string test):base("66666666") {//此处base为异常的Message this.testsss = test; } }
测试:
throw new 异常Ex("测试");
也可以把异常打印在屏幕上:
try { Console.WriteLine("输入"); string a = Console.ReadLine(); if (Convert.ToDouble(a) <= 100) Console.WriteLine("没有错误"); else throw new 异常Ex("测试"); } catch (异常Ex e) { Console.WriteLine(e.Message); Console.WriteLine(e.Testsss); }
注意:
当使用try,catch时,程序可以继续运行
当直接抛出异常时,会即刻中断运行