Debug.Assert的VS异常 [英] Debug.Assert vs Exceptions

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

问题描述

令人惊讶的我才能够找到一个previous问题上的SO关于这个话题,我只是想获得社会信任投票(或不!)对我的做法。

Surprisingly I was only able to find one previous question on SO about this subject, and I'd just like to get the community "Vote of Confidence" (or not!) on my approach.

我看到它的方式是这样的:

The way I see it is thus:

  • 使用 Debug.Assert的来的状态的事情你期望将是真实的。这在一个方法将被使用时,我们是在完全控制我们的环境,例如 验证一些pre和后置条件。
  • 在当前特殊情况下出现使用异常。与外部资源,如文件,数据库,网络等,亦是一个没有脑子。但是...
  • use Debug.Assert to state things you EXPECT would be true. This would be used when we are in complete control over our environment, for example in a method to verify some pre and post-conditions.
  • use Exceptions when exceptional circumstances arise. Dealing with external resources, i.e. files, databases, networks etc is a no-brainer. But...

不慎以下情形有点阴暗。请注意,这是一个人为的例子只用于说明目的!

It gets a little murky in the following scenario. Please note that this is a CONTRIVED EXAMPLE for illustration only!

假设我们有MyClass类,它有一个公共财产MyMode和方法 GetSomeValueForCurrentMode()。考虑MyClass的一心打算运(释放建)在一个库中,供其他开发人员。

Say we have class MyClass, which has a public property MyMode and a method GetSomeValueForCurrentMode(). Consider MyClass as one intended to be shipped (release built) in a library for use by other developers.

我们预计MyMode通过这个类的外部用户进行更新。现在, GetSomeValueForCurrentMode()具有以下逻辑:

We expect MyMode to be updated by external users of this class. Now, GetSomeValueForCurrentMode() has the following logic:

switch(MyMode)
{
case Mode.ModeA:
return val1;
case Mode.ModeB:
return val2;
default:
//Uh-uh this should never happen

}

什么,我在这里获得的是MyClass的用户已经离开它处于无效状态。那么我们该怎么办?

What I'm getting at here is that the user of MyClass has left it in an invalid state. So what should we do?

在默认情况下,我们应该 Debug.Assert的抛出新的InvalidOperationException异常(或其他)?

In the default, should we Debug.Assert or throw new InvalidOperationException (or other) ?

有一个口头禅,说我们不应该相信我们班级的用户。如果我们选择Debug.Assert的,并内置MyClass的一个发布版本(从而去除调试断言)之类的用户将无法获得有用的信息,他们已经处于无效状态离开它。但是,这有点违背了它说只抛出异常时,事情完全失控的情况发生其他的口头禅。

There is one mantra that says we should not trust users of our classes. If we choose Debug.Assert and built MyClass as a release build (thereby removing the Debug Asserts) the user of the class wouldn't get helpful information that they had left it in an invalid state. But it's sort of contrary to the other mantra which says only throw exceptions when things completely out of your control happen.

我觉得我去团团转本 - 这些编程争论也似乎没有一个明确的正确的答案之一。因此,让我们把它付诸表决!

I find I go round in circles with this - one of those programming debates that don't seem to have a definitive 'correct' answer. So let's put it to the vote!

编辑:我注意到了有关的SO问题,这种反应(<一href="http://stackoverflow.com/questions/117171/design-by-contract-tests-by-assert-or-by-exception">design合同测试由断言或异常):

I noticed this response in a related SO question (design by contract tests by assert or by exception?):

经验法则是,你应该想抓住别人的错误时使用的,当你试图抓住自己的错误异常断言和。换句话说,你应该使用异常检查preconditions的公共API函数,每当你来说是外部系统的任何数据。您应该使用断言的是内部系统的功能或数据。

The rule of thumb is that you should use assertions when you are trying to catch your own errors, and exceptions when trying to catch other people's errors. In other words, you should use exceptions to check the preconditions for the public API functions, and whenever you get any data that are external to your system. You should use asserts for the functions or data that are internal to your system.

对我来说,这是有道理的,并且可以与下面介绍的断言再扔的方法。

To me, this makes sense, and can be coupled with the 'Assert then throw' technique outlined below.

思考欢迎!

推荐答案

我同意大多数人在这里和后续的设计按合同。你应该尝试区分得很清楚的设计过程中部署code要求(合同),并计算出预期的状态(调试断言)之间。

I agree with most people here and follow Design-by-Contract. You should try and differentiate very clearly between requirements in deployed code (Contracts) and figuring out expected state during design (Debugging Assertions).

您应该总是抛出合同断言异常(他们应该永远是例外)。有建于大多数框架追赶调试断言机制。但在运行时,你应该总是抛出异常。

You should ALWAYS throw contract assertions as exceptions (as they should always be exceptional). There are mechanisms built in to most frameworks for catching debug assertions. But at runtime you should always throw an exception.

我用一个自定义库,以帮助这个(在C#/ VB.NET)。我最近提出了它在codePLEX( http://www.contractdriven.com/ ),如果你感兴趣的是如何工作的实践。

I use a custom library to help with this (in C#/VB.NET). I recently put up it up on Codeplex (http://www.contractdriven.com/) if you're interested in how this works in practice.

这样做的另一个好处是,当你开始更频繁地使用DbC的,你很少需要使用调试,因为有写在你的code已经明确保证断言,所以它实际上很难获得在无效州。

A side benefit of this is that as you start using DbC more regularly, you seldom need to use debugging assertions as there are already explicit guarantees written in to your code, so it's actually difficult to get in to an invalid state.

因此​​,在你原来的职位的问题...什么我在这里得到的是MyClass的用户已经离开它处于无效状态。所以,我们该怎么办?......不应该出现的。

So the question in your original post... "What I'm getting at here is that the user of MyClass has left it in an invalid state. So what should we do?"...should never arise.

您可能永远不需要任何重新调试! ; - )

You may never need to debug anything again! ;-)

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

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