抛出异常VS Contract.Requires< T&GT ;? [英] Throwing an exception vs Contract.Requires<T>?

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

问题描述

我不知道我是否应该抛出异常或者致电 Contract.Requires< TException>

I'm wondering whether should I throw exceptions or call Contract.Requires<TException>

例如:

public static void Function(String str)
{
    if (str == null) throw new ArgumentNullException("str", "Input string cannot be null.");

    // ...
}



VS

vs

public static void Function(String str)
{
    Contract.Requires<ArgumentNullException>(str != null, "Input string cannot be null.");

    // ...
}



由于<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/dd782896.aspx相对=nofollow> Contract.Requires< TException> 不需要 CONTRACTS_FULL 符号,我可以把它放在我的发布版本也是如此。

Since Contract.Requires<TException> doesn't require the CONTRACTS_FULL symbol I can keep it in my release builds as well.

这是我的考虑:

缺点::你不能调用自定义异常类型构造函数的重载版本。我们根本没有办法通过其他参数的构造函数

Con: You can't call an overloaded version of the custom exception type constructor. There is simply no way to pass additional parameters to the constructor.

临:静态工具的支持(如通知契约违背的调用者)。

Pro: Static tools support (e.g. inform the caller of contract violation).

哪一个我应该使用,以及什么样的情况?

Which one should I use, and for what kind of situation?

推荐答案

基本的权衡之间的 IF-THEN掷要求< TException> 记载于CodeContract用户指南是你如何建立与您的发行位。

The basic trade-off between if-then-throw and Requires<TException> as documented in the CodeContract user guide is how you build with your release bits.

案例1 :您只能使用 IF-THEN掷,没有要求< TException> 。在这种情况下,你可以不上您的DLL / EXE运行合同工具建立自己的发行版位。其优点是,你必须更快的构建,并没有风险的工具引入了错误。第二个好处是,团队成员可以选择不使用CodeContract工具。缺点是你需要没有合同继承和你的合同不一定工具可见的(除非你使用 EndContract )。通过使用装配模式指明这种情况下:自定义参数验证

Case 1: You only use if-then-throw, no Requires<TException>. In this case you can build your release bits without running the contract tools on your dll/exe. The advantage is that you have faster builds and no risk that the tool introduces bugs. A second advantage is that team members can opt out of using the CodeContract tools. Disadvantages are that you get no contract inheritance of requires, and your contracts are not necessarily visible to the tools (unless you use EndContract). You specify this case by using assembly mode: Custom Parameter Validation

案例2 :你决定在你的版本位,始终运行CodeContract工具。这允许您使用要求< TException> ,你会得到合同的继承,包括界面仪器仪表等你的合同是干净的工具识别。缺点是,每个人都建立你的代码必须安装工具CodeContracts。通过使用装配模式指明这种情况下:在合同属性面板标准

Case2: You decide to run the CodeContract tools on your release bits always. This lets you use Requires<TException> and you get inheritance of contracts, including instrumentation of interfaces etc. Your contracts are clean and tool recognizable. The disadvantage is that everyone building your code must have the CodeContracts tools installed. You specify this case by using assembly mode: Standard in the Contract property pane.

希望这清楚的事情了。

这篇关于抛出异常VS Contract.Requires&LT; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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