为什么不捕获一般异常

为什么不捕获一般异常

本文介绍了为什么不捕获一般异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 VS 刚刚告诉我;

My VS just told me;

警告 2 CA1031:Microsoft.Design:修改Program.Main(string[])"以捕获比Exception"更具体的异常或重新引发异常.

我为什么要这样做?如果我这样做了,并且没有捕获所有异常来处理它们,那么我的程序会在广受欢迎的报告屏幕上崩溃.我不希望我的用户遇到这样的错误!

Why should I do that? If I do so, and don't catch all exceptions to handle them, my program crashes with the all-popular report-screen. I don't want my users to get such error-crap!

为什么我不应该一次捕获所有异常以向用户显示一个很好的警告说:出了点问题,不要关心它,我会处理它,请耐心等待"?

Why should I not catch all exceptions at once to display a nice warning to the user saying: "Something went wrong, don't care about it, I will handle it, just be patient"?

编辑:刚刚看到我在这里被骗了,对此感到抱歉欺骗

Edit: Just saw I have a dupe here, sorry for that Dupe

Edit2:澄清事情;在捕获到任何异常后,我会退出程序!我只是不希望我的用户看到在控制台应用程序中引发未处理的异常时出现的向微软报告"对话框!

Edit2: To clarify things; I do exit the program after any exception has been catched! I just don't want my user to see that "report to microsoft" dialog that show up when an unhandled exception is raised in a console-application!

推荐答案

吞下异常是一种危险的做法,因为:

Swallowing exceptions is a dangerous practice because:

  • 它可能导致用户在实际失败时认为某事成功了.
  • 它可以将您的应用程序置于您没有计划的状态.
  • 它使调试变得复杂,因为当您处理奇怪/损坏的行为而不是堆栈跟踪时,要找出故障发生的位置要困难得多.

正如您可能想象的那样,其中一些结果可能是极其灾难性的,因此正确地做到这一点是一个重要的习惯.

As you can probably imagine, some of these outcomes can be extremely catastrophic, so doing this right is an important habbit.

最佳做法

首先,防御性地编写代码,这样就不会发生不必要的异常.它们的计算成本很高.

First off, code defensively so that exceptions don't occur any more than necessary. They're computationally expensive.

尽可能在粒度级别处理预期的异常(例如:FileNotFoundException).

Handle the expected exceptions at a granular level (for example: FileNotFoundException) when possible.

对于意外异常,您可以执行以下两种操作之一:

For unexpected exceptions, you can do one of two things:

  • 让它们正常冒泡并导致崩溃
  • 抓住他们,优雅地失败

优雅地失败?

假设您在 ASP.Net 中工作,并且您不希望向您的用户显示黄屏死机,但您也不希望对开发团队隐藏问题.

Let's say you're working in ASP.Net and you don't want to show the yellow screen of death to your users, but you also don't want problems to be hidden from the dev team.

在我们的应用程序中,我们通常在 global.asax 中捕获未处理的异常,然后进行记录并发送通知电子邮件.我们还展示了一个更友好的错误页面,可以在 web.config 中使用 customErrors 标签进行配置.

In our applications, we usually catch unhandled exceptions in global.asax and then do logging and send out notification emails. We also show a more friendly error page, which can be configured in web.config using the customErrors tag.

这是我们的最后一道防线,如果我们最终收到一封电子邮件,我们会立即采取行动.

That's our last line of defense, and if we end up getting an email we jump on it right away.

这种类型的模式与仅仅吞咽异常相同,在这种情况下,你有一个空的 Catch 块,它的存在只是为了假装"没有发生异常.

That type of pattern is not the same as just swallowing exceptions, where you have an empty Catch block that only exists to "pretend" that the exception did not occur.

其他说明

在 VS2010 中,出现了一种称为 Intellitrace 的东西,它可以让您真正将应用程序状态通过电子邮件发送回家并逐步执行代码,在异常发生时检查变量值等等.这将非常有用.

In VS2010, there's something called intellitrace coming that will allow you to actually email the application state back home and step through code, examine variable values at the time of the exception, and so on. That's going to be extremely useful.

这篇关于为什么不捕获一般异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:26