using System; public class MyException:Exception { public string s; public MyException():base() { s=null; } public MyException(string message):base() { s=message.ToString(); } public MyException(string message,Exception myNew):base(message,myNew) { s=message.ToString();// Stores new exception message into class member s } public static void Test() { string str,stringmessage; bool flag=false; stringmessage=null; char ch=' '; int i=0; Console.Write("Please enter some string (less than 27 characters) - "); str=Console.ReadLine(); try{ ch=str[i]; while (flag==false) { if (ch=='\r') { flag=true; } else{ ch=str[i]; i++; } } } catch(Exception e){ flag=true; } if (i>27) { stringmessage="你的输入不能超过27个字 !"; throw new MyException(stringmessage); } } public static void Main() { try { Test(); } catch(MyException e) { Console.WriteLine(e.s); } } } |