Click here to Skip to main content
15,881,173 members
Articles / Web Development / ASP.NET

Error page for ASP.NET application unhandled exceptions

Rate me:
Please Sign up or sign in to vote.
3.56/5 (10 votes)
14 Jun 2006CPOL2 min read 62K   1K   37   6
Every application needs to show a user-friendly message that something went wrong, along with exception details and the stack trace for the developers. In this article, I am going to show how to display a user-friendly error message.

Introduction

Every application needs to show user-friendly messages when something goes wrong, along with exception details and a stack trace for the developers. In this article, I am going to show how to display a user-friendly error message.

Solution

There are many pieces of the puzzle, and it's very easy to plug them together. Following are the pieces of the puzzle:

  • Message class: It stores the message details you need to display for the developer.
  • Global.asax: Traps the unhandled errors; for every unhandled exception, the Application_Error event is triggered.
  • Error page: To display error to the user.
  • Web.config: Last but the least, the settings to redirect the exception to the error page.

The Message class is the easiest step; create a class Message and add LastException as a static property. I'll come back to static later on when explaining the multithreading issue. In Global.asax, in the Application_Error event, write this code:

C#
Message.LastException = Server.GetLastError().GetBaseException();
string message = "Error Caught in Application_Error event\n" +
        "Error in: " + Request.Url.ToString() +
        "\nError Message:" + Message.LastException.Message.ToString() +
        "\nStack Trace:" + Message.LastException.StackTrace.ToString();

The first line gets the last exception and saves in the Message class' LastException property. The error page is very simple, and has a link to display the message details useful for the developer. The code for this project is attached, so please download the code for testing. Here is piece of code that is included in the hyperlink click event:

C#
if (this.MessagePanel.Visible)
{
    this.MessagePanel.Visible = false;
    this.DetailLinkButton.Text = "Show Details";
}
else
{
    if (Message.LastException != null)
    {
        this.MessageTextBox.Text = "Error Caught in Application_Error event\n" +
            "Error in: " + Request.Url.ToString() +
            "\n\nError Message:" + Message.LastException.Message.ToString() +
            "\n\nStack Trace:" + Message.LastException.StackTrace.ToString();
    }
    else
    {
        //this should never happen.
        this.MessageTextBox.Text = "There is no exception.";
    } 
    this.MessagePanel.Visible = true;
    this.DetailLinkButton.Text = "Hide Details";
}

The above code is very simple, and displays the exception details in a textbox. Web.config: Here is the thing everybody knows, redirect to the error page.

XML
<customErrors mode="On" defaultRedirect="showError.aspx">
</customErrors>

Add the above configuration to the web.config in the system.web section.

That's all, your application is ready to handle exceptions. Oh that multi-user, threading issue. As the Message class has this static property, there is a chance that the exception details may not be correct. It can be fixed by synchronization, but I guess you don't need it unless you have thousands of simultaneous users.

Conclusion

I have explained just one way of showing the exceptions, and there are millions of other ways to do the same.

Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
I am a software architect/developer, having 15 years of design/development experience. Currently working in large financial corporation, architecting .NET applications and coding using C#, ASP.NET, Java Script etc..

MS Computers, MCP, MCTS: .NET Framework 2.0 Web Applications.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ajith_joseph8-Jul-12 22:41
Ajith_joseph8-Jul-12 22:41 
GeneralMy vote of 2 Pin
gopichandana18-Nov-10 19:04
gopichandana18-Nov-10 19:04 
GeneralBut a web site is a multi-user service... Pin
msmits18-Jun-06 23:22
msmits18-Jun-06 23:22 
Hi there,

I work on a website with peeks of > 100K hits/hour (at which time exceptions are not exceptional). In my world multi-threading issues are extremely important.
Your solution may introduce a race condition (which as you point out is easily fixed by adding a lock or Interlocked.Exchange to change the variable) but it may also show errors that belong to a different user.
This cannot be fixed since there is a time gap between setting the error variable and calling the error page.
In my view the best way to trace exceptions is by logging them, and disabling custom pages for developer machines.

Just my opinion Wink | ;-)
Cheers,
Michel
GeneralI agree Pin
tomcat116-Jun-06 4:57
tomcat116-Jun-06 4:57 
Generalwow Pin
eggsovereasy15-Jun-06 10:34
eggsovereasy15-Jun-06 10:34 
GeneralRe: wow Pin
tomcat19-Jul-07 12:00
tomcat19-Jul-07 12:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.