Debug.Assert与异常 [英] Debug.Assert vs Exceptions

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

问题描述

令人惊讶的是,我只能在这个问题上找到一个关于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.

我看到的方式是:


  • 使用 Debug.Assert 来表示你期望的事情是真实的。当我们完全控制我们的环境时,这将被使用,例如在
    验证某些前后条件的方法中。

  • 在出现异常情况时使用异常。处理外部资源,即文件,数据库,网络等是一个没有意义的事情。但是...在以下情况下,它会变得有点暗淡。

请注意,这是一个例外的示例!

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构建为版本生成(从而删除Debug Asserts),则该类的用户将无法获得有用的信息,以使它们处于无效状态。但是这与其他的咒语是有些相反的,只有当事情完全脱离了你的控制之后,才会抛出异常。

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问题(通过合同使用断言或异常设计):

I noticed this response in a related SO question (Design by contract using assertions or exceptions?):


经验法则是,当您尝试捕获自己的错误时,您应该使用断言,以及在尝试捕获其他人的错误时使用异常。换句话说,您应该使用异常来检查公共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.

对我来说,这是有道理的,可以耦合使用下面概述的Assert then throw技术。

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

欢迎光临!

推荐答案

我同意这里的大多数人,并按照合同设计。您应该在部署代码(合同)中的需求和设计之间确定预期状态(调试断言)之间进行非常明确的区分。

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时,很少需要使用调试断言,因为已经有明确的保证写在你的代码中,所以实际上很难进入无效状态。

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

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