如果在执行期间抛出异常,如何描述没有副作用的方法?

时间:2021-12-28 15:37:29

I just can't remember the terminology used for this and other related properties.

我只是记不起用于此和其他相关属性的术语。

EDIT - Maybe such a concept doesn't exist but I remember reading something in Effective C++ (or More Effective C++) where he advocated using swaps to commit changes last in the function because the vector swap functions were guaranteed not to throw an exception.

编辑 - 也许这样的概念不存在,但我记得在有效的C ++(或更有效的C ++)中阅读了一些内容,他提倡使用交换在函数的最后提交更改,因为向量交换函数保证不会抛出异常。

3 个解决方案

#1


2  

I think you mean to say that the function is "exception-safe". See e.g. http://en.wikipedia.org/wiki/Exception_safety.

我想你的意思是说这个功能是“异常安全的”。参见例如http://en.wikipedia.org/wiki/Exception_safety。

The Wikipedia article further divides the safety into various levels. This is the one that is relevant here:

*的文章进一步将安全性分为不同层次。这是相关的:

2. Commit or rollback semantics, also known as strong exception safety or no-change guarantee: Operations can fail, but failed operations are guaranteed to have no side effects so all data retain original values.

2.提交或回滚语义,也称为强异常安全或无变更保证:操作可能失败,但失败的操作保证没有副作用,因此所有数据都保留原始值。

There's a reference to an STL design document that introduces exception safety and commit-or-rollback semantics: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1077.asc

有一个引用异常安全和提交或回滚语义的STL设计文档的引用:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1077.asc

#2


2  

You might be thinking of an exception guarantee, in particular the "strong guarantee".

您可能会考虑异常保证,特别是“强保证”。

#3


1  

Having read your edit, I suspect your title is slightly incorrect. Do you really mean it has no side effects whether or not an exception is thrown (as per the current wording) or that it has no side effects when an exception is thrown, but if the method completes without throwing an exception then it will/can have side effects? There's a pretty big difference :)

阅读完编辑后,我怀疑你的标题有点不正确。你是否真的意味着它没有副作用,无论是否抛出异常(根据当前的措辞)或抛出异常时它没有副作用,但如果方法完成而没有抛出异常那么它将/可以有副作用吗?有一个很大的区别:)

The latter is described in Accelerated C# 2008 as "exception neutrality".

后者在Accelerated C#2008中被描述为“异常中立”。

#1


2  

I think you mean to say that the function is "exception-safe". See e.g. http://en.wikipedia.org/wiki/Exception_safety.

我想你的意思是说这个功能是“异常安全的”。参见例如http://en.wikipedia.org/wiki/Exception_safety。

The Wikipedia article further divides the safety into various levels. This is the one that is relevant here:

*的文章进一步将安全性分为不同层次。这是相关的:

2. Commit or rollback semantics, also known as strong exception safety or no-change guarantee: Operations can fail, but failed operations are guaranteed to have no side effects so all data retain original values.

2.提交或回滚语义,也称为强异常安全或无变更保证:操作可能失败,但失败的操作保证没有副作用,因此所有数据都保留原始值。

There's a reference to an STL design document that introduces exception safety and commit-or-rollback semantics: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1077.asc

有一个引用异常安全和提交或回滚语义的STL设计文档的引用:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1077.asc

#2


2  

You might be thinking of an exception guarantee, in particular the "strong guarantee".

您可能会考虑异常保证,特别是“强保证”。

#3


1  

Having read your edit, I suspect your title is slightly incorrect. Do you really mean it has no side effects whether or not an exception is thrown (as per the current wording) or that it has no side effects when an exception is thrown, but if the method completes without throwing an exception then it will/can have side effects? There's a pretty big difference :)

阅读完编辑后,我怀疑你的标题有点不正确。你是否真的意味着它没有副作用,无论是否抛出异常(根据当前的措辞)或抛出异常时它没有副作用,但如果方法完成而没有抛出异常那么它将/可以有副作用吗?有一个很大的区别:)

The latter is described in Accelerated C# 2008 as "exception neutrality".

后者在Accelerated C#2008中被描述为“异常中立”。