views:

1304

answers:

7

Coming from a desktop background I'm not sure exactly how to pass the exceptions I have caught to an Error page in order to avoid the standard exception screen being seen by my users.

My general question is how do I pass the exception from page X to my Error page in ASP.net?

A: 

I think you can use the global.asax -- Application_Exception handler to catch the exception and then store it for displaying in an error page.

But actually, your error page shouldn't contains code that might cause just another error. It should be simple "Oops! something went wrong" page.

If you want details on the error, use Windows' events viewer or ELMAH or employ some logging mechanism.

chakrit
I hate "Oops! something went wrong" with a passion. There should be some trivial error handling around an error reporting page that ultimately might cascade to an "Oops" page, but if you reach that Oops page nothing should be running at all. I would like to give telligent's coders and whoever else is guilty of this a wedgie and a swirly, at the same time.
stimpy77
@stimpy77 It depends... Sometimes that's what the event logs and ELMAH are for... and the error page is just something to calm users down.
chakrit
+6  A: 

I suggest using the customErrors section in the web.config:

   <customErrors mode="RemoteOnly" defaultRedirect="/error.html">
      <error statusCode="403" redirect="/accessdenied.html" />
      <error statusCode="404" redirect="/pagenotfound.html" />
   </customErrors>

And then using ELMAH to email and/or log the error.

Forgotten Semicolon
+1  A: 

Use the custom error pages in asp.net, you can find it in the customError section in the web.config

Darren Kopp
+2  A: 

The pattern I use is to log the error in a try/catch block (using log4net), then do a response.redirect to a simple error page. This assumes you don't need to show any error details.

If you need the exception details on a separate page, you might want to look at Server.GetLastError. I use that in global.asax (in the Application_Error event) to log unhandled exceptions and redirect to an error page.

David
+2  A: 

We've had good luck capturing exceptions in the Global.asax Application_Error event, storing them in session, and redirecting to our error page. Alternately you could encode the error message and pass it to the error page in the querystring.

marc
+2  A: 

You can also get the exception from

Server.GetLastError();
Mladen Mihajlovic
+1  A: 

We capture the exception in the Global.asax file, store it in Session, the user is then redirected to the Error Page where we grab the exception for our Session variable and display the Message information to the user.

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        this.Session[CacheProvider.ToCacheKey(CacheKeys.LastError)] = ex;
    }

We do log the error message prior to displaying it the user.

mattruma
I tried this, but it doesn't work when I use Session["Error"] on global.asax and the aspx page.
Junior Mayhé