主要方法代码完全在 try/catch 中:这是不好的做法

时间:2023-03-31
本文介绍了主要方法代码完全在 try/catch 中:这是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我将我所有的 Main 方法代码放在一个 try/catch 块中,如下所示:

Usually I put all of my Main method code inside of a try/catch block like so:

public static void Main(string[] args)
{
   try
   {
      // code
   }
   catch (Exception e)
   {
      // code
   }
}

我这样做是为了以防任何异常从程序逻辑的其余部分中溜走,从而允许我做一些事情,例如将其显示到控制台、将其记录到文件等.但是,我被告知这是不好的做法.

I do this just in case any exceptions manage to slip out of the rest of the program logic, thus allowing me to do something about it, such as display it to console, log it to a file, etc. However, I have been told that this is bad practice.

你认为这是不好的做法吗?

Do you think it is bad practice?

推荐答案

try/catch任何段代码> 没有充分理由的阻止是不好的做法.

在 .NET 编程模型中,应为真正的异常情况或条件保留异常.您应该尝试捕获您实际上可以做某事的异常.此外,您不应该永远捕获基本的 System.Exception 类(而是更愿意捕获您可以的更具体的派生异常类处理).如果在您的程序执行过程中遇到真正意外的异常,您实际上应该崩溃.

In the .NET programming model, exceptions should be reserved for truly exceptional cases or conditions. You should only try to catch exceptions that you can actually do something about. Furthermore, you should should hardly ever catch the base System.Exception class (but rather prefer to catch the more specific, derived exception classes you can handle). And should a truly unexpected exception be encountered during the course of your program's execution, you actually should crash.

显然,正确"的答案必须根据具体情况做出,具体取决于 catch//code 占位符内部发生的情况> 阻止.但是,如果您要求一般规则或最佳实践",您应该始终有一个特定的理由来捕获异常,而不仅仅是将所有代码包装在一个巨大的 try/catch 理所当然地阻塞而不考虑它.

Obviously the "correct" answer would have to be made on a case-by-case basis, depending on what's going on inside that // code placeholder in your catch block. But if you're asking for a general rule or "best practice", you should always have a specific reason to catch exceptions, not just wrap all of your code in a giant try/catch block as a matter of course without thinking about it.

请注意,如果您只是为了记录日志或错误报告而尝试捕获任何可能发生的未处理异常,则应该使用 AppDomain.UnhandledException 事件.这是一个仅通知事件,因此它不允许您处理这些异常,但它是在应用程序崩溃后实现日志记录或错误报告功能的正确位置.

Note that if you're simply trying to catch any unhandled exceptions that might occur for the purposes of logging or error reporting, you should be using the AppDomain.UnhandledException event. This is a notification-only event, so it doesn't allow you to handle those exceptions, but it is the right place to implement your logging or error reporting functionality after your application has crashed.

当我正在阅读 Raymond Chen 的优秀博客时,旧新事物",我注意到他最近发表了一篇关于类似主题的文章.它特定于 COM,而不是 .NET Framework,但有关错误处理的一般概念同样适用于这两种环境.我想我会在这里分享这篇文章中的一些精华,以支持我的[显然颇有争议]的观点.

As I was catching up on my reading of Raymond Chen's excellent blog, "The Old New Thing", I noticed that he had recently published an article on a similar topic. It's specific to COM, rather than the .NET Framework, but the general concepts regarding error handling are equally applicable to both environments. I thought I'd share a couple of gems from the article here, in support of my [apparently quite controversial] opinion.

从历史上看,COM 在服务器的方法周围放置了一个巨大的 try/except.如果您的服务器遇到通常未处理的异常,巨大的 try/except 会捕获它并将其转换为错误 RPC_E_SERVERFAULT.然后它将异常标记为已处理,以便服务器保持运行,从而即使遇到问题也能保持服务器运行,从而提高鲁棒性."

Historically, COM placed a giant try/except around your server's methods. If your server encountered what would normally be an unhandled exception, the giant try/except would catch it and turn it into the error RPC_E_SERVERFAULT. It then marked the exception as handled, so that the server remained running, thereby "improving robustness by keeping the server running even when it encountered a problem."

请注意,这实际上是一种伤害.

Mind you, this was actually a disservice.

发生未处理的异常意味着服务器处于意外状态.通过捕获异常并说别担心,一切都很好",您最终会让损坏的服务器继续运行.

The fact that an unhandled exception occurred means that the server was in an unexpected state. By catching the exception and saying, "Don't worry, it's all good," you end up leaving a corrupted server running.

[ ...]

捕获所有异常并让进程继续运行假定服务器可以从意外故障中恢复.但这是荒谬的.您已经知道服务器无法恢复:它崩溃了!

Catching all exceptions and letting the process continue running assumes that a server can recover from an unexpected failure. But this is absurd. You already know that the server is unrecoverably toast: It crashed!

更好的是让服务器崩溃,以便可以在故障点捕获崩溃转储.现在你有机会弄清楚发生了什么.

Much better is to let the server crash so that the crash dump can be captured at the point of the failure. Now you have a fighting chance of figuring out what's going on.

您可以 [并且应该] 在他的博客上阅读整篇文章:如何关闭 COM有用地"包裹在服务器周围的异常处理程序.

You can [and should] read the whole article here on his blog: How to turn off the exception handler that COM "helpfully" wraps around your server.

这篇关于主要方法代码完全在 try/catch 中:这是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:C# 中的静默失败,看似未处理的异常,不会使程 下一篇:在 try { return x; 中真正发生了什么?} 最后 { x = n

相关文章

最新文章