ASP.NET中的全局错误处理 [英] Global error handling in ASP.NET
问题描述
我的经验已经排除了一些非常具体的情况(如事务),我们正在撰写的大多数ASP.NET应用程序都沿着
void ButtonEventHandler(object sender,EventArgs e){
Page.Validate();
if(Page.IsValid){
//通过相关的数据库插入或更新数据库。
//如果它的一个事务,那么我们在内部回滚并重新抛出
//异常。
}
}
为什么不只有一个全局异常处理程序?通常(在这一点上),我唯一可以做的就是优雅地中止操作,并告诉用户再试一次。
处理未捕获的例外的全球地点将在 Global.asax处理的Application_Error
。正如约翰指出的那样,你应该尽可能地将异常处理尽可能靠近它们发生的位置并作出适当的反应。
Is there anything wrong with catching errors in an ASP.NET application globally (eg: Global.asax)? I have viewed the question Good error handling practice and it doesn't really say too much about this.
My experience has been excluding some very specific circumstances (such as transactions) most of the ASP.NET applications we are writing are along the lines of
void ButtonEventHandler(object sender, EventArgs e) {
Page.Validate();
if (Page.IsValid) {
//Do a database insert or update thru relevant datalayers.
//If its a transaction then we rollback internally and rethrow
//the exception.
}
}
Why not just have a global exception handler? Usually (at this point) the only thing I can do is abort the operation gracefully and tell the user to try again.
The global place to handle uncatched Exceptions would be in Global.asax by handling Application_Error
. As John pointed out, you should always handle exceptions as close as possible to where they might occur and react appropriately.
这篇关于ASP.NET中的全局错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!