首页 > 应用开发 > IT综合 > c#中构建异常处理(转)
| 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==) { 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); } } } |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8781179/viewspace-922484/ ,如需转载,请注明出处,否则将追究法律责任。