try_catch异常捕获
static void Main(string[] args) { int intNum = 0; bool bFlag = true; Console.WriteLine("请输入一个数字:"); try { intNum = Convert.ToInt32(Console.ReadLine()); } catch { Console.WriteLine("输入数据异常,程序退出!"); bFlag = false; } if (bFlag) { Console.WriteLine("the number is:{0}", intNum); } Console.ReadKey(); }