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

Error page for ASP.NET application unhandled exceptions

, 14 Jun 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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";
}
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.

<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)

Share

About the Author

tomcat1
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 PinmemberAjith_joseph8-Jul-12 23:41 
GeneralMy vote of 2 Pinmembergopichandana18-Nov-10 20:04 
GeneralBut a web site is a multi-user service... Pinmembermsmits19-Jun-06 0:22 
GeneralI agree Pinmembertomcat116-Jun-06 5:57 
Generalwow Pinmembereggsovereasy15-Jun-06 11:34 
You need to use the little code boxes.
 
Also half the point of the friendly error page is to keep details of the crash away from potential hackers (who can use the error from a SQL injection attack to more quickly figure out your db). I would either log the error to the data base (in the Application_Error handler in global.asax) and/or email it to the developer.
GeneralRe: wow Pinmembertomcat19-Jul-07 13: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 | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 14 Jun 2006
Article Copyright 2006 by tomcat1
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid