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

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

问题描述

这有什么错,在ASP.NET应用程序中捕捉错误全球范围内(如:Global.asax中)?我已经看到了问题良好的错误处理实践以及它并没有真正说太多关于这个

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.

我的经验已经排除了一些非常特殊的情况(如交易)大多数我们正在编写ASP.NET应用程序都是沿

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.

推荐答案

全球排名来处理uncatched的例外是在的 Global.asax中通过处理的Application_Error 。正如约翰指出,你应该总是处理尽可能接近的地方,他们可能会发生,并且做出适当的反应异常。

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天全站免登陆