ASP.NET中的全局错误处理

Max*_*ich 9 c# asp.net error-handling

在全局ASP.NET应用程序中捕获错误是否有任何问题(例如:Global.asax)?我已经看过一个好的错误处理实践的问题,并没有真正说太多.

我的经验是排除了一些非常具体的情况(例如事务),我们编写的大多数ASP.NET应用程序都是如此

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.
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么不只有一个全局异常处理程序?通常(此时)我唯一能做的就是优雅地中止操作并告诉用户再试一次.

Ica*_*rus 7

处理未捕获的异常的全局位置将通过处理Application_ErrorGlobal.asax中.正如约翰所指出的那样,你应该尽可能地将异常尽可能地接近它们可能发生的位置并做出适当的反应.