1 namespace ConsoleApplication3 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 try 8 { 9 //int a = Convert.ToInt32("abc"); 10 string m= WrongMsg(280); 11 Console.WriteLine(m); 12 } 13 catch (Exception ex) 14 { 15 Console.WriteLine("错误消息:"+ex.Message+"错误位置:"+ex.StackTrace); 16 } 17 Console.ReadKey(); 18 } 19 static string WrongMsg(int age) 20 { 21 if (age > 0 && age < 3) 22 return "婴幼儿"; 23 else if (age > 3 && age < 18) 24 return "青少年"; 25 else if (age > 18 && age < 150) 26 return "成年人"; 27 else { 28 throw new Exception("外星人"); 29 } 30 } 31 } 32 }