views:

206

answers:

3

I'm looking into re-working and simplifying our error handling in an application I support. We currently have all of our pages inheriting from a base class we created, which in turn obviously inherits from System.Web.UI.Page. Within this base class, the OnError method is currently being overridden, and in turn calling MyBase.OnError, and then calling one of our custom logging methods.

I don't see any benefit of overriding the OnError method, and I think it would be better to let the Application_Error method in the Global.asax take care of the unhandled exception (logging it) and then the customErrors section in the config would trigger a process to redirect the user.

Looking online it looks like people override this method quite frequently, but I don't see a need to and this article from MSDN makes me think the same.

+1  A: 

Hey,

I create a custom class called PageBase:

public class PageBase : Page
{
  protected override void OnError(..)
  {
     //handle error, redirect to error page
  }
}

And so I only have to do it once, and use that to catch unhandled errors and redirect to the error page. This way I have to do it once; I don't know that Page.Error event has any pros or cons over application error; but I use the page error as it can be convenient here; I can clear the error and redirect to the error page right within the context of the page... my personal preference.

Thanks for the MSDN link; that was a pretty good resource.

HTH.

Brian
But, again, why override the OnError method? If you need to handle information within the context of the page, I understand why you'd want to handle the error within that context (logging specific data about the page, user input, display messages on the screen, etc), but why override the existing OnError method instead of using Page_Error or Application_Error?
Ek0nomik
page_error originates from onerror (Page_Load is called from OnLoad method, etc.), so that's why OnError; its the same as Page_Error... So the real question is why page error vs. app error...
Brian
With the link that I provided above, the paragraph above the "global.asax: Application_Error" section mentions that putting code into an OnError Override method isn't the same as using Page_Error.So, in your case I suppose you are overriding that functionality to redirect on your own, so it's probably okay. In my case, we aren't really overriding any of that functionality within the OnError method, we're just doing very general logging about the exception that was thrown. I think that we could put the logging at the application level and remove our OnError method. Thanks for your input.
Ek0nomik
NP, for what it was worth. I think that article said there is more going on with OnError, right? Well, here is the thing, if you reflect in the .NET code, OnError is defined in TemplateControl, and OnError simply fires the error event... I didn't see it being override in the Page class, so I don't know that that resource is correct... I could be wrong...
Brian
A: 

I could see a scenario where maybe only some pages in the application inherit from the base class and need to handle errors differently. All other errors would be caught/logged by Application_Error

Hugh Jeffner
+1  A: 

I have never overriden the OnError method. I like to use the Application_Error of the global asax, which in turn will catch any pages that might not inherit from your base class. Furthermore, overriding a method is used to change it's functionality, so if you're not doing so I wouldn't override it.

Also, I know it's not part of your question, but I would look at ELMAH for error logging:

http://code.google.com/p/elmah/

Jonas Stawski