Debug.Assert与异常投掷 [英] Debug.Assert vs Exception Throwing

查看:163
本文介绍了Debug.Assert与异常投掷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读了很多文章(以及在StackOverflow上发布的其他几个类似的问题)关于如何和何时使用断言,我理解他们很好。但是我仍然不明白什么样的动机应该让我使用 Debug.Assert 而不是抛出一个纯粹的异常。我的意思是,在.NET中,对失败断言的默认响应是停止世界,并向用户显示一个消息框。虽然这种行为可以修改,但我觉得这是非常烦人和冗余的
来做到这一点,而我可以反而只是抛出一个合适的异常。这样,我可以在我抛出异常之前很容易地将错误写到应用程序的日志中,而且,我的应用程序不一定会冻结。

I've read plenty of articles (and a couple of other similar questions that were posted on StackOverflow) about how and when to use assertions, and I understood them well. But still, I don't understand what kind of motivation should drive me to use Debug.Assert instead of throwing a plain exception. What I mean is, in .NET the default response to a failed assertion is to "stop the world" and display a message box to the user. Though this kind of behavior could be modified, I find it highly annoying and redundant to do that, while I could instead, just throw a suitable exception. This way, I could easily write the error to the application's log just before I throw the exception, and plus, my application doesn't necessarily freeze.

那么为什么要我,如果有的话,使用 Debug.Assert 而不是一个纯粹的异常?把一个断言置于不应该的地方可能只会导致各种不想要的行为,所以在我看来,我真的没有通过使用断言而不是抛出异常来获得任何东西。你是否同意我的看法,或者我在这里缺少一些东西?

So, why should I, if at all, use Debug.Assert instead of a plain exception? Placing an assertion where it shouldn't be could just cause all kinds of "unwanted behavior", so in my point of view, I really don't gain anything by using an assertion instead of throwing an exception. Do you agree with me, or am I missing something here?

注意:我完全理解理论上有什么区别(Debug vs释放,使用模式等),但正如我所看到的,我会更好地抛出异常而不是执行断言。因为如果在生产版本中发现了一个错误,我仍然希望断言将失败(毕竟,开销是可笑的),所以我会更好地抛出一个异常。

Note: I fully understand what's the difference "in theory" (Debug vs Release, usage patterns etc.), but as I see it, I would be better off throwing an exception instead of performing an assert. Since if a bug is discovered on a production release, I still would like that the "assertion" would fail (after all, the "overhead" is ridiculously small), so I'll be better off throwing an exception instead.

编辑:我看到的方式,如果一个断言失败,这意味着该应用程序输入了一些一种破坏,意想不到的状态。那么为什么要继续执行?无论应用程序是运行在调试还是发布版本都无关紧要。

The way I see it, if an assert failed, that means that the application entered some kind of corrupted, unexpected state. So why would I want to continue execution? It doesn't matter if the application runs on a debug or release version. The same goes to both

推荐答案

尽管我同意您的推理是可信的如果一个断言被意外地违规,那么通过抛出来停止执行是有道理的 - 我个人不会在断言的地方使用异常。这就是为什么:

Though I agree that your reasoning is plausible -- that is, if an assertion is violated unexpectedly, it makes sense to halt execution by throwing -- I personally would not use exceptions in the place of assertions. Here's why:

正如其他人所说,断言应该 不可能的情况,以这样一种方式如果据称不可能的情况通过,开发商将被通知。相比之下,异常提供了异常,不太可能或错误的情况,但不是不可能的情况下的控制流程机制。对我而言,主要区别在于:

As others have said, assertions should document situations that are impossible, in such a manner that if the allegedly impossible situation comes to pass, the developer is informed. Exceptions, by contrast, provide a control flow mechanism for exceptional, unlikely, or erroneous situations, but not impossible situations. For me, the key difference is this:


  • 应该总是可以产生一个测试用例, 。如果不可能产生这样的测试用例,那么你的程序中的代码路径将永远不会执行,并且应该被删除为死代码。

  • It should ALWAYS be possible to produce a test case which exercises a given throw statement. If it is not possible to produce such a test case then you have a code path in your program which never executes, and it should be removed as dead code.

绝对不可能产生导致断言触发的测试用例。如果断言触发,则代码错误或断言错误;无论哪种方式,代码中都需要更改。

It should NEVER be possible to produce a test case which causes an assertion to fire. If an assertion fires, either the code is wrong or the assertion is wrong; either way, something needs to change in the code.

这就是为什么我不会用一个异常替换断言。如果断言无法实际触发,则将其替换为异常表示您的程序中有一个不可测试的代码路径。我不喜欢不可测试的代码路径。

That's why I would not replace an assertion with an exception. If the assertion cannot actually fire, then replacing it with an exception means you have an untestable code path in your program. I dislike untestable code paths.

这篇关于Debug.Assert与异常投掷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆