ASP.NET中的全局错误处理 [英] Global error handling in ASP.NET

查看:165
本文介绍了ASP.NET中的全局错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在全球ASP.NET应用程序中捕获错误(例如:Global.asax)有什么问题吗?我已经查看了良好的错误处理实践的问题,并没有太多关于这个



我的经验已经排除了一些非常具体的情况(如事务),我们正在撰写的大多数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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆