Search code examples
asp.netpage-lifecycleonerror

When to override OnError?


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.


Solution

  • 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.