Saturday, August 16, 2008

How to create a global error capturing mechanism for your web site

All web sites should include a general error handling code in case anything unexpected happens.

Creating this mechanism is really simple. Add Global Application Class (Global.asax) to your project if it doesn't already include one. Create the following method in the class if there isn't one.

 

void Application_Error(object sender, EventArgs e)
    {  

       Exception LastException=Server.GetLastError();
        //Your error handling code here.
        Server.ClearError();

    }

 

If the exception is not handled in the application, the above method gets called. You can call Server.GetLastError() method in order to get the last exception. After your error handling code, you should make a call to Server.ClearError() method to clear the exception.

No comments: