Introduction

Recently, while going through exception handling and logging, I found an interesting topic that I would like to share, "Exceptions", that is a family member of every language/technology used. These are sometimes irritating for developers. If it's irritating for developers, what would be the condition of the end user when he/she views the yellow screen populated with God knows what!.

Now the question is, should they be displayed with that entire stack trace that is sometimes helpful for developers to resolve errors? The answer obviously is no.

Here is a small tip that might be handy. Here, I am trying to detail the use of "Custom Errors" and its attributes and elements. Web.config, the main settings and configuration for an ASP.NET web application, is the file where the custom errors find its existence. According to MSDN, custom error elements provide information about custom error messages. The main motive here is to display the end-user custom error pages. First, let's understand how the custom errors are written in the web.config (as we know in XML format):

<customerrors mode="On">

The Modes Used

On

Off

Remote Only

This is the best among all for the developers' perspectives, since this allows the Remote clients to view the custom error messages/pages.

Allows the local users, especially developers, to view the ASP.NET errors.

This is the default value.

Some More Facts

Another attribute that is used is "defaultRedirect". This is an optional attribute that is used to redirect the browser to the default error page if any, else generic errors are shown to the users.

<customerrors defaultredirect="Error/Index" mode="">

  1. <customerrors defaultredirect="" mode="">
  2. <error statuscode="400" redirect="NotFound.htm">
  3. <error statuscode="500" redirect="InternalServerError.htm">

Another important thing to note is Custom Errors that can be defined in the following two levels:

We also handle exceptions in Global.asax, in other words using:

  1. protected void Application_Error(Object sender, EventArgs e)
  2. {
  3. Exception ex = Server.GetLastError(); //self explanatory gets the most recent error
  4. Server.ClearError(); //self explanatory clears the error
  5. //(Required to clear as otherwise user gets to see the default ASP.NET error handlers)
  6. Response.Redirect(""); //default redirect.
  7. }

Now the precedence is as follows.

Quote:

When all are defined, the Page Level will have the higher precedence than global.asax and customErrors. And if the customErrors are also defined as well as in Global.asax, then customErrors will have no effect and if no exception handling is done in the global.asax, then Web.config that is customErrorscome into action.

Points of Interest

Note

Here, I end this. Thanks for reading. I hope you learned something from this.