简体   繁体   English

C#重载尝试/捕获

[英]C# overload try/catch

Is it possible to overload the try/catch statements in C#? 是否可以在C#中重载try / catch语句? Failing that, is it possible to inherit and make similar functionality? 如果失败,是否有可能继承并实现类似的功能?

We are adding ELMAH to an existing web application and the code uses try/catches extensively, displaying errors in the validation summary instead of displaying error pages. 我们将ELMAH添加到现有的Web应用程序中,并且代码广泛使用try / catches ,在验证摘要中显示错误,而不是显示错误页面。 As the errors are being caught in the catch statement ELMAH assumes that the errors are being dealt with; 由于在catch语句中捕获了错误,因此ELMAH假定正在处理错误;因此,错误代码将被视为错误。 in actuality they are just being displayed in a more presentable manner. 实际上,它们只是以一种更具表现力的方式进行显示。

Here is a sample of what it is doing: 以下是其工作示例:

try
{
    //code here...
}
catch (Exception ex)
{
    customValidatorError.IsValid = false;
    customValidatorError.ErrorMessage = ex.Message;
}

Ideally the overload would just tell Elmah to add the exception to the logs via ErrorSignal.FromCurrentContext().Raise(e); 理想情况下,重载只是告诉Elmah通过ErrorSignal.FromCurrentContext().Raise(e);将异常添加到日志中ErrorSignal.FromCurrentContext().Raise(e);

Due to the frequency this technique is used in the project I would prefer not to add that code to each and every time it is used. 由于这种技术在项目中使用的频率很高,所以我宁愿不要在每次使用时都添加该代码。 It is also beyond the scope of the project to add custom error pages for each error page instead of adding the errors to the validation summary. 为每个错误页面添加自定义错误页面,而不是将错误添加到验证摘要中也超出了项目的范围。

As you've found, wrapping everything in try/catch usually doesn't turn out to be what you really want. 正如您所发现的,将所有内容包装在try / catch中通常并不是您真正想要的。 You end up "swallowing" exceptions that you probably want to log somewhere. 您最终可能会“吞噬”异常,而这些异常可能要记录在某个地方。 If you do want to show the exception message on the page with a custom validator, you should probably re-throw the exception so that you can bubble that up. 如果您确实想在页面上使用自定义验证器显示异常消息,则可能应该重新引发异常,以便您可以对此进行冒泡。 ASP.NET will allow you to setup "global" error handling. ASP.NET将允许您设置“全局”错误处理。 If you want to swallow the exceptions, you can do that in a single place. 如果您想吞下例外,可以在一个地方进行。 If you want to signal ELMAH or log them using another method, you can do so in a single place. 如果要发信号通知ELMAH或使用其他方法记录日志,则可以在单个位置进行。 This keeps your exception handling DRY. 这使您的异常处理保持DRY。

Pro tip: Use throw; 专家提示:使用throw; rather than throw ex; 而不是throw ex; . This will keep the original stack trace. 这将保留原始堆栈跟踪。

Use a logging library like log4net to log such exceptions (logging them is indeed not handling them directly, but you are using exception handling - as soon as you have caught an exception it is deemed "handled"). 使用诸如log4net之类的日志记录库来记录此类异常(记录它们的确不是直接处理它们,但是您正在使用异常处理-一旦捕获到异常,便视为“已处理”)。

ELMAH is used for unhandled exceptions, as you have seen. 如您所见,ELMAH用于未处理的异常。

The two approaches complement each other (ELMAH + logging library). 两种方法相互补充(ELMAH +日志记录库)。

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

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