简体   繁体   English

在C#中替换try…catch块

[英]Replace try…catch block in c#

Is there any possibility/code other than using try...catch block for exception handling and further logging. 除了使用try ... catch块进行异常处理和进一步记录外,是否有其他可能性/代码。 We are developing a Web application with couple of screen. 我们正在开发带有几个屏幕的Web应用程序。 Client asked us to not to put try...catch blocks in each functions/methods to handle errors. 客户要求我们不要在每个函数/方法中放置try ... catch块来处理错误。 Please suggest. 请提出建议。

Thanks 谢谢

We are developing a Web application with couple of screen 我们正在开发带有几个屏幕的Web应用程序

Although the requirement of you client does not make much sense for me but you can handle the unhandled exception using Application_Error event in global.asax 尽管您的客户端要求对我而言意义不大,但是您可以使用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. 此代码示例演示如何在Global.asax文件中创建错误处理程序,该错误处理程序将在处理请求时捕获所有未处理的ASP.NET错误,换句话说,所有未通过Try / Catch块或在捕获/捕获块中捕获的错误。页面级错误处理程序。 In the example, the handler transfers control to a generic error page named GenericErrorPage.aspx, which interprets the error and displays an appropriate message. 在示例中,处理程序将控制权转移到名为GenericErrorPage.aspx的通用错误页面,该页面解释错误并显示适当的消息。

As a additional Note for Desktop application like WinForms or Window Services you can using AppDomain.CurrentDomain.UnhandledException or Application.ThreadException etc. 作为WinForms或Window Services之类的桌面应用程序的附加说明,您可以使用AppDomain.CurrentDomain.UnhandledExceptionApplication.ThreadException等。

You can use Application_Error in global.asax, This method handles the error for all the pages in the application. 您可以在global.asax中使用Application_Error,此方法可处理应用程序中所有页面的错误。

for specific page for example default.aspx, you can implementing Page_Error 对于特定页面(例如default.aspx),您可以实现Page_Error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM