之前很差好考慮異常,測試的時候麻煩太多,簡記一下實體層的簡單小異常,告誡本身一下,初學者也可參考一下ide
自定義異常類:測試
- /// <summary>
- ///Exception 的摘要說明
- /// </summary>
- public class Exception1:Exception
- {
- public Exception1(string msg) : base(msg) { }
- public Exception1( string s,Exception e) :base( s, e ) {}
- }
實體類拋異常:spa
- namespace TOOL
- {
- public class model
- {
- private int _dxbh;
- public int DHHM
- {
- set
- {
- try
- {
- _dxbh = value;
- string dh = @"^\d{5}$";
- if (!Regex.IsMatch(_dxbh.ToString(), dh))
- {
- throw new Exception1("電話號碼錯誤");
- }
- }
- catch( Exception e1)
- {
- throw e1;
- }
- }
- get
- {
- return _dxbh;
- }
- }
- }
- }
頁面捕獲xml
- protected void Page_Load(object sender, EventArgs e)
- {
- try
- {
- model m = new model();
- m.DHHM = 1;
- }
- catch (Exception1 e1)
- {
- TextBox1.Text = e1.Message;
- Response.Write("<script>alert('" + e1.Message + "');</script>");
- }
- }