Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding try/catch hell in my web pages

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

like image 444
Shaun_web Avatar asked Jan 28 '26 16:01

Shaun_web


2 Answers

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.

like image 124
marapet Avatar answered Feb 01 '26 04:02

marapet


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.

like image 41
derek Avatar answered Feb 01 '26 05:02

derek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!