开发者

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


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.


Check out global.asax documentation. The global.asax file contains the global method for handling errors. When an error occurs, the Application_Error method is called. This method is a good place to handle and log errors because it is the most functional.


I'd recommend using something like this, instead of throwing ecxceptions. It sounds like you are doing exception-based programming

if(myTextBox.Text == "foo")
{
  this.RaiseError(); // method where you tell a label or something, where you say the user that and error (though no exception!) occured
}

The thing you are looking for is the CustomError property in the web.config

<customErrors mode="On"    defaultRedirect="~/error.aspx" />

So if there is an unhandeld exception, the user gets redirect to the error.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜