Wednesday, March 24, 2010

Custom Error Handling in ASP.NET

.Net provides number of ways to manage and handle errors. Generally we use try..catch block to handle the error and show the error message in a label. Instead of showing the error in that page, we can show the error in another page with the customized manner, so that it can be seem more professional.

Steps:

1.Create an ASP.NET web site.
2.Add a class ErrorPage.cs in the App_Code folder.
3.Add the following variable in the ErrorPage.cs file

//Declare the private HttpApplication variable
private HttpApplication _application;

void IHttpModule.Init (HttpApplication application)
{
_application = application;

//check whether the ErrorHandling values is Application type set in the web.config
//file or not. If yes then declare an EventHandler 
if (GetValue("ErrorHandling") == "Application")
_application.Error += new EventHandler(ErrorPageHandler);

}


private void ErrorPageHandler(Object sender, EventArgs e)
{
//fetch the error
ProcessException(_application.Server.GetLastError());
}

//store the error in the Cache
private void ProcessException(Exception exception)
{
HttpContext.Current.Cache["Error"] = HttpContext.Current.Request.Url.ToString();
HttpContext.Current.Cache["ErrorMsg"] = exception.Message;
HttpContext.Current.Cache["StackTrace"] = exception.StackTrace;

HttpContext.Current.Response.Redirect(GetValue("SiteUrl") + "/Error.aspx");
}


//fetch the value from the web.config file
public string GetValue(string Key)
{
string ret = System.Web.Configuration.WebConfigurationManager.AppSettings[Key];

return ret;
}
public void Dispose()
{
}


4.Create an Error.aspx page and add the following html code
<asp:table width="100%" align="center" >

<asp:tr>
<asp:td style="text-align: left; font-weight: bold">
Error In
<asp:/td>
<asp:/tr>
<asp:tr>
<asp:td  style="text-align: left; font-weight: bold">
<asp:asp:Label ID="lblError" runat="server" CssClass="x-panel-mc-font"><asp:/asp:Label>
<asp:/td>
<asp:/tr>
<asp:tr>
<asp:td  style="text-align: left; font-weight: bold">
  
<asp:/td>
<asp:/tr>
<asp:tr>
<asp:td style="text-align: left; font-weight: bold">
Error Message
<asp:/td>
<asp:/tr>
<asp:tr>
<asp:td  style="text-align: left;">
<asp:asp:Label ID="lblErrorMsg" runat="server" CssClass="x-panel-mc-font"><asp:/asp:Label>
<asp:/td>
<asp:/tr>
<asp:tr>
<asp:td  style="text-align: left;">
  
<asp:/td>
<asp:/tr>
<asp:tr>
<asp:td style="text-align: left; font-weight: bold">
Stack Track
<asp:/td>
<asp:/tr>
<asp:tr>
<asp:td  style="text-align: left;">
<asp:asp:Label ID="lblStackTrace" runat="server" CssClass="x-panel-mc-font"><asp:/asp:Label>
<asp:/td>
<asp:/tr>
<asp:tr>
<asp:td  style="text-align: left;">
  
<asp:/td>
<asp:/tr>
<asp:/table>



Here I have used 3 lables to show the page name (from where error is generated), Error Message and Stack Track.

5.Add the following code in the Error.aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
if (Cache["Error"] != null)
lblError.Text = Cache["Error"].ToString();
if (Cache["ErrorMsg"] != null)
lblErrorMsg.Text = Cache["ErrorMsg"].ToString();
if (Cache["StackTrace"] != null)
lblStackTrace.Text = Cache["StackTrace"].ToString();

}


Fetched the errors from the Cache and showing in the labels.

6.In the default.aspx page add a button control, and add the following code in the button click event.

int a = 1, b = 0;
int c = a / b;


Generating the error.

7.Add the following setting in the web.config file

<asp:appSettings>
<asp:add key="SiteUrl" value="http://localhost:1371/CustomeErrorHandling"/>
<asp:add key="ErrorHandling" value="Application"/>
<asp:/appSettings>


And add in the <httpModules> section

<asp:add type="ErrorPage" name="ErrorPageHandler">


8.Now run the application



9.Click on the Show Error button, then the error will be show in the following manner.



Note: Do not use try..catch block in the code behind page, If you use then custome error will not work. i.e. If I write the code in the following manner
try
{
int a = 1, b = 0;
int c = a / b;
}
catch
{ }

Then the error page will not show.

Download :

No comments:

Post a Comment