限制 C++ 允许的投掷有什么好处? 功能?

发布于 2024-07-14 10:12:56 字数 273 浏览 12 评论 0原文

声明 C++ 函数可能抛出的异常有什么好处? 换句话说,添加关键字 throw() 实际上有什么作用?

我读过诸如 void do_something() throw(); 这样的函数声明应该保证 do_something() 函数不会产生任何异常; 然而,这似乎不适用于在 do_something() 中调用的函数,因此它的保证很弱。

请概述此语言功能的有用性(和最佳用例)。

What is the benefit of declaring the possible exception-throws from a C++ function? In other words, what does adding the keyword throw() actually do?

I've read that a function declaration such as void do_something() throw(); should guarantee that no exceptions originate from the do_something() function; however, this doesn't seem to hold true of functions called within do_something(), thus making it a weak guarantee.

Please outline the usefulness (and best-use cases) of this language feature.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听风吹 2024-07-21 10:12:56

没有人比 Sutter

http://www.ddj.com/architect/184401544

更好 地解释这一点简短版本是

  1. 永远不要编写异常规范,
  2. 除非可能是空的

No one explains this better than Sutter

http://www.ddj.com/architect/184401544

The short version is

  1. Never write an exception specification
  2. Except possibly an empty one
甜点 2024-07-21 10:12:56

C++ 标准要求,如果函数尝试抛出不在其异常列表中的异常,则调用 unexpected() 函数。 MSDN 对此的简短描述如下: http:// msdn.microsoft.com/en-us/library/awbt5tew(VS.80).aspx

大多数编译器实际上并不支持此 C++ 功能。

The C++ standard requires that the unexpected() function is called if a function attempts to throw an exception that is not on its exception list. A short description of this from MSDN is here: http://msdn.microsoft.com/en-us/library/awbt5tew(VS.80).aspx

Most compilers do not actually support this C++ feature.

宣告ˉ结束 2024-07-21 10:12:56
void do_something() throw(); 

这是实现者方面的保证,即该函数永远不会抛出异常。 这就是该函数的客户端可以所期望的。 然而,这也意味着函数内部某处生成的任何异常都会被处理,而不是重新抛出回 do_something() 的父级。 对那些从内部重新抛出 eth 的人感到羞耻,因为没有什么可以阻止他抛出。 这是因为,从具有空异常规范的函数内部抛出相当于抛出规范中未提及的异常,并使 std::unexpected() 是预期的,然后程序终止。

BTW:普遍接受的强异常保证的定义是:如果公共操作由于某种原因失败,则抛出异常,并且对象的状态保持不变(原子操作)。

void do_something() throw(); 

This is a guarantee from the implementer's side that the function will never throw an exception. That is what a client of the function can expect. However, it also means any exception generated somewhere inside the function is handled and not rethrown back to the parent of do_something(). Shame on he who re-throw-eth from within, for there is nothing that'll stop him from throwing. This is because, throwing from within a function with an empty exception-specification is equivalent to throwing an exception not mentioned in the specification and making the std::unexpected() is to be expected followed by program termination.

BTW: The generally accepted definition of strong exception guarantee is: If a public operation fails for some reason, an exception is thrown, and the object's state is left unchanged (atomic operation).

随梦而飞# 2024-07-21 10:12:56

基本上,通过使用 throw(); 声明函数,您可以告诉编译器您百分百确定该函数不会运行抛出任何异常,允许编译器执行一些优化。 如果您的函数无论如何都会引发异常,那么您就会遇到麻烦,因为它很可能会导致未定义的行为。

Basically, by declaring the function with throw(); you tell the compiler that you are one hundred per cent certain that the function is not going to throw any exceptions, allowing the compiler to perform some optimizations. If your function throws an exception anyway, you're in trouble, since it most likely results in undefined behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文