`void func() throw(type)` 有什么意义?

发布于 08-08 10:12 字数 183 浏览 14 评论 0原文

我知道这是一个有效的 C++ 程序。 函数声明中的 throw 有何意义? AFAIK 它什么都不做,也不用于任何用途。

#include <exception>
void func() throw(std::exception) { }
int main() { return 0; }

I know this is a valid c++ program.
What is the point of the throw in the function declarement? AFAIK it does nothing and isnt used for anything.

#include <exception>
void func() throw(std::exception) { }
int main() { return 0; }

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

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

发布评论

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

评论(6

清风疏影2024-08-15 10:12:12

它指定任何 std::exception 都可以从 func() 抛出,而不能抛出任何其他异常。如果抛出其他内容,它将调用 unexpected() 函数,该函数默认调用 terminate()

这意味着抛出其他内容几乎肯定会终止程序,就像未捕获的异常一样,但实现必须强制执行此操作。这通常与在 func() 周围放置 try{...}catch(){...} 块非常相似,这会抑制性能。

通常,根据本周大师专栏,异常规范是不值得的它。 Boost 指南 指出,空白 < 可能会有一点好处code>throws() 对于非内联函数,也有缺点。

It specifies that any std::exception can be thrown from func(), and nothing else. If something else is thrown, it will call an unexpected() function which by default calls terminate().

What this means is that throwing something else will almost certainly terminate the program, in the same manner as an uncaught exception, but the implementation will have to enforce this. This is normally much the same as putting a try{...}catch(){...} block around func(), which can inhibit performance.

Usually, exception specifications aren't worth it, according to the Guru of the Week column about it. The Boost guidelines say that there might be a slight benefit with a blank throws() for a non-inline function, and there are disadvantages.

背叛残局2024-08-15 10:12:12

这是一个例外规范,几乎可以肯定这是一个坏主意

它指出 func 可能会抛出 std::exception,并且 func 发出的任何其他异常都将导致调用 unexpected()

That is an exception specification, and it is almost certainly a bad idea.

It states that func may throw a std::exception, and any other exception that func emits will result in a call to unexpected().

谈场末日恋爱2024-08-15 10:12:12

这是 C++ 异常规范。它声明特定函数可能会抛出 std::exception 类型。

一般来说,C++ 中的异常规范被认为是需要避免的功能。这是一个奇怪的特性,因为它的行为是在编译时声明的,但仅在运行时检查(与 Java 的版本非常不同)。

这是一篇很好的文章,详细介绍了该功能

This is a C++ exception specification. It declares that the particular function will potentially throw a std::exception type.

In general though exception specifications in C++ are considered a feature to avoid. It's an odd feature in that it's behavior is declared at compile time but only checked at runtime (very different from say Java's version).

Here is a good article which breaks down the feature

纵山崖2024-08-15 10:12:12

这是一个异常规范。它表示 func() 可以抛出的唯一异常是 std::exception (或其派生物)。尝试抛出任何其他异常将导致 std::unexpected。

This is an exception specification. It says that the only exception that func() can throw is std::exception (or a derivative thereof). Attempting to throw any other exception will give std::unexpected instead.

贩梦商人2024-08-15 10:12:12

异常规范。 throw 关键字后面的类型准确指定函数可以抛出的所有异常(如果有)。见草案15.4。

注意:没有异常规范的函数允许所有异常。具有空异常规范 throw() 的函数不允许任何异常。

Exception specification. The type(s) following the throw keyword specifies exactly what all, if any, exceptions the function can throw. See 15.4 of the draft.

Note: A function with no exception-specification allows all exceptions. A function with an empty exception-specification, throw(), does not allow any exceptions.

口干舌燥2024-08-15 10:12:12

基本上是这样的:

void func() throw(std::exception,B) {  /* Do Stuff */}

只是这样的简写:

void func()
{
    try
    {
        /* Do Stuff */ 
    }
    catch(std::exception const& e)
    {
        throw;
    }
    catch(B const& e)
    {
        throw;
    }
    catch(...)
    {
        unexpected();  // This calls terminate
                       // i.e. It never returns.
    }
}

调用终止()很少是您想要的,因为堆栈没有展开,因此您在 RAII 中的所有努力都被浪费了。该规则的唯一例外是声明一个空的抛出列表,这主要用于文档目的,以表明您支持不抛出异常保证(在这种情况下您仍然应该手动捕获所有异常)。

一些重要的(恕我直言)不应该抛出的地方是析构函数和 swap() 方法。析构函数很少被显式标记为“不抛出”,但 swap() 经常被标记为“不抛出”。

void myNoThrowFunc() throws()  // No-Throw (Mainlly for doc purposes).
{
    try
    {
        /* Do Stuff */
    }
    catch(...)  // Make sure it does not throw.
    {
        /* Log and/or do cleanup */
    }
}

Basically this:

void func() throw(std::exception,B) {  /* Do Stuff */}

Is just shorthand fro this:

void func()
{
    try
    {
        /* Do Stuff */ 
    }
    catch(std::exception const& e)
    {
        throw;
    }
    catch(B const& e)
    {
        throw;
    }
    catch(...)
    {
        unexpected();  // This calls terminate
                       // i.e. It never returns.
    }
}

Calling terminate() is rarely what you want, as the stack is not unwound and thus all your efforts in RAII is wasted. The only exception to the rule is declaring an empty throw list and this is mainly for documentation purposes to show that you are supporting the no-throw exception gurantee (you should still manually catch all exceptions in this situation).

Some important (imho) places that should be no-throw are destructors and swap() methods. Destructors are rarely explicitly marked no-throw but swap() are quite often marked no-throw.

void myNoThrowFunc() throws()  // No-Throw (Mainlly for doc purposes).
{
    try
    {
        /* Do Stuff */
    }
    catch(...)  // Make sure it does not throw.
    {
        /* Log and/or do cleanup */
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文