Debug.Assert的VS异常抛出 [英] Debug.Assert vs Exception Throwing

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

问题描述

我读过很多文章的(一对夫妇其他的类似的被张贴在计算器上那个)有关如何以及何时使用断言,我明白他们很好的问题。不过,我仍然不明白什么样的动机应该开车送我使用 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?

注意:我完全理解什么是理论上的区别(调试VS发布,使用模式等),但在我看来,我会过得更好抛出一个异常,而不是执行断言。因为如果一个bug在生产发行后发现,我还是想的说法会失败(毕竟开销是小得可笑),所以我会更好抛出一个异常,而不是

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:


  • 应该总是能够生产出行使一个给定的throw语句测试用例。如果它是不可能产生这样的测试情况,那么你必须在你的程序,从来没有执行代码路径,它应该是死代码被删除。

  • 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的VS异常抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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