自定义Exception

public class HonException:ApplicationException 
{
    public HonException()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    private string hong;
    public HonException(string hon)
    {
        hong = hon;
        HttpContext.Current.Response.Write("hong:" + hong+"<br>");
    }
    public string Hong
    {
        get { return hong; }
        set { hong = value; }
    }
}
  protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            
        }
        catch (HonException h)
        {
            Response.Write(h.Hong + "<br>");
        }
        catch (Exception ee)
        {
            Response.Write(ee.Message + "<br>");
        }
        finally
        {
            Response.Write("over!<br>");
        }
    }

  protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            throw new HonException("hongdada");
        }
        catch (HonException h)
        {
            Response.Write(h.Hong + "<br>");
        }
        catch (Exception ee)
        {
            Response.Write(ee.Message + "<br>");
        }
        finally
        {
            Response.Write("over!<br>");
        }
    }

代码很简单,意思很明确,

自定义异常由自己抛出捕获

捕获异常以后进入catch,再执行finally

 protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            throw new HonException("hongdada");
            throw new HonException("hongdada2");
            Response.Write("Last<br>");
        }
        catch (HonException h)
        {
            Response.Write(h.Hong + "<br>");
        }
        catch (Exception ee)
        {
            Response.Write(ee.Message + "<br>");
        }
        finally
        {
            Response.Write("over!<br>");
        }
    }

一个注意点:

在using中如果抛出错误,最后还是要执行释放对象的

 public class Program
    {
        static void Main(string[] args)
        {
            try
            {
                new Hong();
                using (new Hong())
                {
                    throw new Exception();
                    Console.WriteLine("using");
                }
            }
            catch  (Exception ee)
            {
            }
            Console.Read();
        }
         
    }
    public class Hong : IDisposable
    {
        static Hong()
        {
            Console.WriteLine("1");
        }
        public Hong()
        {
            Console.WriteLine("constructor");
        }
        public void Dispose()
        {
            Console.WriteLine("2");
        }
    }

如果是if

static void Main(string[] args)
        {
            try
            {
                new Hong();
                if (true)
                {
                    throw new Exception();
                    Console.WriteLine("if");
                }
            }
            catch  (Exception ee)
            {
            }
            Console.Read();
        }

 

自定义异常要放在Exception上面

http://blog.csdn.net/zevin/article/details/6901489

http://blog.sina.com.cn/s/blog_66aeefeb010127i2.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值