ASP.Net异常的捕获小结

四种方法:

  1. 建立一个PageBase (class),自定义OnError事件
  2. 在Global.asax中自定义Application_Error事件
  3. 在web.config中定义新的页面
    <customErrors mode="On" defaultRedirect="~/errors/GeneralError.aspx"/>

    自定义GeneralError.aspx捕获异常
  4. 在HttpModule中自定义OnError事件

例子:

protected override void OnError(EventArgs e)
{
// At this point we have information about the error
HttpContext ctx = HttpContext.Current;

Exception exception = ctx.Server.GetLastError ();

string errorInfo = "
Offending URL: " + ctx.Request.Url.ToString () + "
Source: " + exception.Source + "
Message: " + exception.Message + "
Stack trace: " + exception.StackTrace; ctx.Response.Write (errorInfo); // -------------------------------------------------- // To let the page finish running we clear the error // -------------------------------------------------- ctx.Server.ClearError (); base.OnError (e); }
posted @ 2010-10-18 16:19  KymoWang  阅读(404)  评论(0编辑  收藏  举报