之前很差好考虑异常,测试的时候麻烦太多,简记一下实体层的简单小异常,告诫本身一下,初学者也可参考一下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>");
- }
- }