断言合同失败改变If-Then-Throw行为 [英] Assert on Contract Failure Changes If-Then-Throw Behavior

查看:115
本文介绍了断言合同失败改变If-Then-Throw行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过调用Contract.EndContractBlock()终止传统的If-Then-Throw样式前置条件时,我会根据Assert on Contract Failure选项的值获得不同的行为。启用"合同失败时断言"时,原始的
异常永远不会被抛出。事实上,如果我查看反汇编代码,原始异常就完全被丢弃了。调用__ContractRuntime.Requires()可以根据需要执行断言。

When I have legacy If-Then-Throw style pre-conditions terminated with a call to Contract.EndContractBlock(), I get different behaviors depending on the value of the Assert on Contract Failure option. When Assert on Contract Failure is enabled, the original exception never gets thrown. In fact, if I look at the disassembled code, the original exception has been discarded altogether. Calls to __ContractRuntime.Requires() are made to perform the assertions as necessary.

如果我关闭合同失败时断言选项,原始异常将被保留并抛出,具体取决于结果ContractHelper.RaiseContractEventFailed方法。如果存在处理失败的处理程序,它似乎返回null。否则
抛出原始异常。

If I turn the Assert on Contract Failure option off, the original exceptions are preserved and thrown depending on the result of the ContractHelper.RaiseContractEventFailed method. It seems to return null if a handler exists to handle the failure. Otherwise the original exception is thrown.

我发现这令人困惑。如果我说在合同失败时断言,在RequiresRelease(传统的If-Then-Throw)类型前置条件的断言对话框中,我希望Abort中止,重试附加调试器并忽略掉掉以抛出我原来的
例外。中止和重试按预期工作,但忽略只是进入代码中的下一行。最初的例外不存在。

I find this confusing. If I say to Assert on Contract Failure, in the assertion dialog for RequiresRelease (legacy If-Then-Throw) type pre-conditions, I'd expect Abort to abort, Retry to attach the debugger and Ignore to fall through to throw my original exception. Abort and Retry work as expected but Ignore just proceeds to the next line in the code. And the original exception isn't there.

我没有看到任何地方记录这种行为。这是预期的吗?

I don't see this behavior documented anywhere. Is this expected?

推荐答案

嗨凯文,

感谢您提出此问题。我们在这里的行为确实不一致,我们应该解决这个问题。我同意预期的行为应该是遗产需要选择断言断言应该导致断言,如果你忽略它,那么
应该继续原始抛出。

thanks for raising this issue. We are indeed not consistent in the behavior here and we should fix this. I agree that the expected behavior should be that for legacy requires choosing Assert-of-failure should cause the assertion, and if you ignore it, should continue with the original throw.

我刚刚查看了最新版本,我们做了大量工作来保留验证并使它们与合同验证方法(常用的遗留if-then-throws的缩写)一起使用。我注意到我们不再声称遗留
要求,而只是抛出原始异常。

I just looked at the latest release where we did quite a bit of work to preserve the validations and make them work with contract validation methods (abbreviations for commonly used legacy if-then-throws). I notice that we are no longer asserting for legacy requires and instead just throw the original exception.

BTW,同样适用于Requires< E>(...)。如果选择了assert-on-failure,它将为Assert,如果忽略失败,将继续而不是抛出E。

BTW, the same applies with Requires<E>(...). If assert-on-failure is selected, it will Assert and if you ignore the failure, will continue instead of throwing E.


这篇关于断言合同失败改变If-Then-Throw行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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