asp.net捕獲全域未處理異常的幾種方法

來源:互聯網
上載者:User
1.通過HttpModule來捕獲未處理的異常【推薦】

首先需要定義一個HttpModule,並監聽未處理異常,代碼如下:

        public void Init(HttpApplication context)        {            context.Error += new EventHandler(context_Error);        }        public void context_Error(object sender, EventArgs e)        {            //此處處理異常            HttpContext ctx = HttpContext.Current;            HttpResponse response = ctx.Response;            HttpRequest request = ctx.Request;            //擷取到HttpUnhandledException異常,這個異常包含一個實際出現的異常            Exception ex = ctx.Server.GetLastError();            //實際發生的異常            Exception iex = ex.InnerException;            response.Write("來自ErrorModule的錯誤處理<br />");            response.Write(iex.Message);            ctx.Server.ClearError();        }

 

然後在web.config中加入配置資訊:

        <httpModules>            <add name="errorCatchModule" type="WebModules.ErrorHandlerModule, WebModules" />        </httpModules>

 

這樣就可以處理來自webApp中未處理的異常資訊了。

之所以推薦這種方法,是因為這種實現易於擴充、通用;這種方法也是用的最多的。

 

 

2.Global中捕獲未處理的異常

在Global.asax中有一個Application_Error的方法,這個方法是在應用程式發生未處理異常時調用的,我們可以在這裡添加處理代碼:

        void Application_Error(object sender, EventArgs e)        {            //擷取到HttpUnhandledException異常,這個異常包含一個實際出現的異常            Exception ex = Server.GetLastError();            //實際發生的異常            Exception iex = ex.InnerException;            string errorMsg = String.Empty;            string particular = String.Empty;            if (iex != null)            {                errorMsg = iex.Message;                particular = iex.StackTrace;            }            else            {                errorMsg = ex.Message;                particular = ex.StackTrace;            }            HttpContext.Current.Response.Write("來自Global的錯誤處理<br />");            HttpContext.Current.Response.Write(errorMsg);            Server.ClearError();//處理完及時清理異常        }

 

這種處理方式同樣能夠擷取全域未處理異常,但相對於使用HttpModule的實現,顯得不夠靈活和通用。

HttpModule優先於Global中的Application_Error方法。

 

3.頁面層級的異常捕獲

我們還可以在頁面中添加異常處理方法:在頁面代碼中添加方法Page_Error,這個方法會處理頁面上發生的未處理異常資訊。

        protected void Page_Error(object sender, EventArgs e)        {            string errorMsg = String.Empty;            Exception currentError = Server.GetLastError();            errorMsg += "來自頁面的異常處理<br />";            errorMsg += "系統發生錯誤:<br />";            errorMsg += "錯誤地址:" + Request.Url + "<br />";            errorMsg += "錯誤資訊:" + currentError.Message + "<br />";            Response.Write(errorMsg);            Server.ClearError();//清除異常(否則將引發全域的Application_Error事件)        }

 這種方法會優先於HttpModule和Global。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.