你真的可以有一个没有主体的函数/方法,而只是一个 try/catch 块吗?

2022-01-23 00:00:00 g++ try-catch c++

请注意,此函数没有{"和}"主体.只是一个 try/catch 块:

Note that this function does not have a "{" and "}" body. Just a try/catch block:

void func( void )
try
{
    ...
}
catch(...)
{
    ...
}

这是有意成为 C++ 的一部分,还是 g++ 扩展?

Is this intentionally part of C++, or is this a g++ extension?

除了绕过 1 级 {} 之外,还有其他目的吗?

Is there any purpose to this other than bypass 1 level of {}?

在遇到 http://stupefydeveloper.blogspot.com/2008/10/c-function-try-catch-block.html

推荐答案

是的,这是标准的.函数 try 块,因为它们被称为,对常规函数没有太多用处,但对于构??造函数,它们允许您捕获初始化器列表中抛出的异常.

Yes, it is standard. Function try blocks, as they're called, aren't that much use for regular functions, but for constructors, they allow you to catch exceptions thrown in the initialiser list.

请注意,在构造函数的情况下,异常总是会在任何 catch 块的末尾重新抛出.

Note that, in the constructor case, the exception will always be rethrown at the end of any catch blocks.

相关文章