How to handle errors in APS.NET

There are three ways to handle errors in asp.net application.

1. Using Page class Error even:

You can handle error on every page by define error handler for Page class Error event.
Like-

void Page_Error(object sender, EventArgs e)
{
Response.Redirect("Errorpage.htm");
}

2. By defining error handler in global.asax file

This is better approach than the first one because its a centralize approach to handle errors. Here instead of writing error handler in every web page, you will need to define error handler at application level in the global.asax file.
Like:

void Application_Error(object sender, EventArgs e)
{
Response.Redirect("Errorpage.htm");
}

Note:- Whenever an error occurs application look for error handler at page level and then at application level. So if you have defined error handler in the web pages then it will handle the error not the one defined in global.asax file.

3. By adding customError tag in the web.config file

This is another centralize approach to error handling in ASP.NET. In fact you can redirect to different custom error pages for different error status codes.
Like:

< customErrors defaultRedirect="error.htm" mode="On" >
< error statusCode="404" redirect="notfound.htm"/ >
< /customErrors >

2 comments:

tangguh said...

i love your articles. :)

morp gadgetss said...

thanks for the info