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 globalThere 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 captureWe 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