Tutorial :When to override OnError?



Question:

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:1

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.


Solution:2

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/


Solution:3

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »