Click here to Skip to main content
Click here to Skip to main content

Error page for ASP.NET application unhandled exceptions

By , 14 Jun 2006
Rate this:
Please Sign up or sign in to vote.


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.


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:

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:

if (this.MessagePanel.Visible)
    this.MessagePanel.Visible = false;
    this.DetailLinkButton.Text = "Show Details";
    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();
        //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.

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

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.


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

Happy coding!


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

About the Author

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 PinmemberAjith_joseph8-Jul-12 22:41 
GeneralMy vote of 2 Pinmembergopichandana18-Nov-10 19:04 
GeneralBut a web site is a multi-user service... Pinmembermsmits18-Jun-06 23:22 
GeneralI agree Pinmembertomcat116-Jun-06 4:57 
Generalwow Pinmembereggsovereasy15-Jun-06 10:34 
GeneralRe: wow Pinmembertomcat19-Jul-07 12:00 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 14 Jun 2006
Article Copyright 2006 by tomcat1
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid