替换 c# 中的 try...catch 块

Replace try...catch block in c#

除了使用 try...catch 块进行异常处理和进一步记录外,还有什么 possibility/code 吗? 我们正在开发一个带有几个屏幕的 Web 应用程序。客户要求我们不要在每个 functions/methods 中放置 try...catch 块来处理错误。请提出建议。

谢谢

We are developing a Web application with couple of screen

虽然您客户的要求对我来说没有多大意义,但您可以使用 global.asax 中的 Application_Error 事件处理未处理的异常

void Application_Error(object sender, EventArgs e)
{
     //Your logging code goes here
}

How to: Handle Application-Level Errors

This code example shows how to create an error handler in the Global.asax file that will catch all unhandled ASP.NET errors while processing a request — in other words, all the errors that are not caught with a Try/Catch block or in a page-level error handler. In the example, the handler transfers control to a generic error page named GenericErrorPage.aspx, which interprets the error and displays an appropriate message.

作为 WinForms 等桌面应用程序或 Window 服务的附加说明,您可以使用 AppDomain.CurrentDomain.UnhandledException or Application.ThreadException

您可以在global.asax中使用Application_Error,此方法处理应用程序中所有页面的错误。

对于特定页面,例如 default.aspx,您可以实施 Page_Error