I am writing an ASP.NET website, which is a new framework for me. I find that I have a try/catch block in literally every method of my codebehind. All these try/catch blocks do is catch the exception and then pop-up an error message to the user. Isn't there some sort of global error handler in ASP.NET? It's worth noting that my error handling is within control (ASCX) pages, and I would like a way to simply get each ASCX to handle its own errors without forcing all error handling just to a single master page or a redirect...
Thanks for advice below. I did try Page_Error as it seems a positive option. I ran a test and my exceptions do now run through Page_Error, however thing's ain't quite working... I use 'Content.ClearError()' at the end of my Page_Error, however, the client browser still ends up with an unhandled PageRequestManagerServerErrorException. Any advice? If it helps, I am using Telerik and trying to pop open a radalert whenever an error occurs...
You could implement the Page_Error Event handler at the page level. That sounds like the perfect fit for what you asked.
Otherwise, more globally on the application level, you may try the Application_Error Event Handler in Global.asax.
See also http://msdn.microsoft.com/en-us/library/aa479319.aspx which contains a lot of information on error handling in asp.net.
you can set the CustomErrors node in the web.config. This way whenever an exception occurs, the user is automatically redirected to a friendly error message page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With