Link to home
Start Free TrialLog in
Avatar of akulas
akulas

asked on

Web page exceptions redirect to single page - ASP.NET

The legacy code we have is throwing stack trace exceptions at the users face on multiple occasions,  Need to give a clean page to the users.  How can I intercept the exceptions and redirect to the error page?  We are using ASP.NET.
Avatar of prairiedog
prairiedog
Flag of United States of America image

Remove all Try/Catch blocks from your code to let ASP.NET to throw exceptions. Then use Application_Error sub in global.asax to handle all the exceptions like this:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
	' Fires when an error occurs
 
	Dim oEx As Exception = HttpContext.Current.Server.GetLastError()
 
	' Handle the exception, such as emailing developer, logging error into DB, etc
	
	Response.Redirect("YourGenericErrorPage.aspx")
 
 
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of DropZone
DropZone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would suport handling error using the application error events in global.asax. But there is no need to remove all try .. catch or error handler code from your pages.

If you have a aplication error handler in global.asax, all errors that are not handled will be handled by the code wrote within this event. Here you can log the error in the standard way as you have in your application (maybe writing to a file, sending mail etc) and then redirect the user to a "custom Error" page .. which shows a message that a error has occured and maybe provide a link to take him the "home" page

Rejo
The reason I suggested to remvoe Try/Catch blocks is that, unless you throw that exception again, you will lost stack trace information. Correct me if I am wrong.
I like to let ASP.NET to handle any exception error because all errors will bubble up.
I think the issue the author is facing is not about how to log exceptions, but that in some cases some unhandled exceptions are been shown to the user and this shows the stack trace .. So I assume he already has code to handle exceptions and do the needful as per his application requirement ...
Exactly.  In that case, setting the CustomErrors directive in the Web.Config will cause unhandled exceptions to redirect (external users) to a custom error page.

    -dZ.
I think the author wants to intercept all exception errors in one central place then display a generic error message ("clean page" according to the author's post) to the user.
I agree Dropzone .. there are multiple ways of solving this issue .. i was just trying to point that for handling it using global.asax, there is no need to remove all existing try... catch blocks ..
Well, certainly that's up to him, but he hasn't made that assertion as far as I can tell, so it is just conjecture.

I believe, as Rejojohny mentioned, that he just does not want users to receive the call stack.  Whether he removes all Try/Catch blocks from his code and lets all exceptions float to the top is irrelevant for this; pressumably the errors the users are seeing are the ones that floated to the top of the stack already.

He could, of course, redirect manually in the Application.OnError event, but by using the CustomErrors directive, he can change the target page without having to recompile the entire application.  Plus he can set different pages for different types of errors too (404, etc.).

      Cheers!
      -dZ.
Hum, my last comment was in response to prairiedog.  I agree completely with you, Rejojohny: I don't think removing Try/Catch blocks is need (as I mentioned in my first comment).
akulas,
      I just want to add that, using the CustomErrors directive, you can also set custom error messages for different errors.  For example, you could have a custom "Page not found" page for 404 errors, and a "We're sorry, but something blew up" page for 500 errors.

Here's more information on using the directive:
       http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx

       -dZ.