Several ways that ASP. NET captures global unhandled exceptions

Source: Internet
Author: User

Catch Unhandled Exception "recommended" by HttpModule

First you need to define a httpmodule and listen for unhandled exceptions, the code is as follows:

public void Init (HttpApplication context) {context. Error + = new EventHandler (context_error);} public void Context_error (object sender, EventArgs e) {//Handle exception here HttpContext CTX = HttpContext.Current; HttpResponse response = ctx. Response; HttpRequest request = CTX. request;//gets the httpunhandledexception exception that contains an actual occurrence of the exception exception ex = CTX. Server.GetLastError ();//The exception that actually occurs exception IEX = ex. Innerexception;response. Write ("Error handling from Errormodule <br/>"); response. Write (IEX. Message); ctx. Server.ClearError ();}

Then add the configuration information in Web. config:

This allows handling of unhandled exception information from WebApp.

This approach is recommended because the implementation is easy to extend and generic;

Unhandled exception caught in global

There is a Application_Error method in Global.asax that is called when an unhandled exception occurs in the application, and we can add the processing code here:

void Application_Error (object sender, EventArgs e) {//Get to Httpunhandledexception exception, this exception contains an actual exception exception ex = Server.GetLastError ();//The exception that actually occurs 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 ("Error handling from global <br/>"); HttpContext.Current.Response.Write (ERRORMSG); Server.ClearError ();//Finish cleaning up the exception in time}

This kind of processing can also get the global unhandled exception, but it is not flexible and common compared with the implementation of HttpModule.

The HttpModule takes precedence over the Application_Error method in global.

Page-level exception capture

We can also add exception handling methods to the page: Add method page_error to the page code, which handles unhandled exception information that occurs on the page.

protected void Page_Error (object sender, EventArgs e) {string errormsg = String.Empty; Exception currenterror = Server.GetLastError (); ErrorMsg + = "Exception handling from page <br/>"; errormsg + = "System error: <br/>"; ErrorMsg + = "Error Address:" + request.url + "<br/>"; errormsg + = "error message:" + currenterror.message + "<br/>"; Response.Write (ERRORMSG); Server.ClearError ();//Clear exception (otherwise the global Application_Error event will be raised)}

This approach takes precedence over HttpModule and global.

REFERENCE from:http://www.cnblogs.com/weixing/p/3326060.html

Several ways that ASP. NET captures global unhandled exceptions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.