函数尝试块,但不在构造函数中

时间:2022-03-07 22:19:45

just a quick question. Is there any difference between

只是一个小问题。两者之间有什么区别吗

void f(Foo x) try
{
   ...
}
catch(exception& e)
{
   ...
}

and

void f(Foo x)
{
    try { ... }
    catch (exception& e)
    {
        ...
    }
}

?

吗?

If no, why are function try blocks for (the case of initialization lists for constructors being put aside) ? What happens if the copy constructor of Foo throws an exception when x is passed to f ?

如果没有,为什么函数尝试块(构造函数的初始化列表被放到一边)?当x被传递给f时,如果Foo的复制构造函数抛出异常会发生什么?

4 个解决方案

#1


9  

Function try blocks are only ever needed in constructors. In all other cases exactly the same effect can be achieved by enclosing the entire body of the function in a normal try/catch block.

函数尝试块只在构造函数中需要。在所有其他情况下,通过将整个函数体封装在一个正常的try/catch块中,可以实现完全相同的效果。

If the copy constructor used to initialize a parameter throws an exception this happens before the function call. It cannot be caught by a function try block or exceptional handler in the function as the function doesn't get called.

如果用于初始化参数的复制构造函数抛出异常,这将在函数调用之前发生。它不能被函数try block或函数中的异常处理程序捕获,因为函数不会被调用。

#2


5  

Some things are allowed because it would be harder to disallow them. Allowing function try blocks on some, but not all function bodies would make the grammar and compilers more complicated.

有些事情是允许的,因为不允许就更难了。允许在一些函数体上尝试函数块,但不是所有的函数体都会使语法和编译器更加复杂。

#3


3  

Just spotted an interesting point in this Dr. Dobb's article (although quite old):

刚刚在Dobb博士的文章中发现了一个有趣的点(虽然已经很老了):

...remember that you can't return a value inside a function-try-block handler. So it makes no sense to use a function try block for a non-void function

…请记住,不能在函数-try-block处理程序中返回值。因此,对于非虚函数使用函数try块是没有意义的

and this is their code example:

这是他们的代码示例:

int f()
try
{
   ...
}
catch(Error &e)
{
   // oops, can't return int from here!
}

Which actually means that function try blocks are weaker than "regular" try blocks and their use should be discouraged other than in constructors.

这实际上意味着函数try块比常规的try块要弱,除了构造函数之外,不应该使用它们。

(the article is from 2000, so it'd be nice if someone would comment on whether this is still so in the current standard)

(这篇文章是2000年的,如果有人能评论一下这篇文章在目前的标准下是否仍然如此,那就太好了)

#4


1  

Function try blocks were added expressly for the purpose of catching exceptions in constructor initialization lists.

函数try块是为了捕获构造函数初始化列表中的异常而特意添加的。

In your example there are no constructor initializations, so there is no difference between the two forms.

在您的示例中没有构造函数初始化,因此这两种形式之间没有区别。

#1


9  

Function try blocks are only ever needed in constructors. In all other cases exactly the same effect can be achieved by enclosing the entire body of the function in a normal try/catch block.

函数尝试块只在构造函数中需要。在所有其他情况下,通过将整个函数体封装在一个正常的try/catch块中,可以实现完全相同的效果。

If the copy constructor used to initialize a parameter throws an exception this happens before the function call. It cannot be caught by a function try block or exceptional handler in the function as the function doesn't get called.

如果用于初始化参数的复制构造函数抛出异常,这将在函数调用之前发生。它不能被函数try block或函数中的异常处理程序捕获,因为函数不会被调用。

#2


5  

Some things are allowed because it would be harder to disallow them. Allowing function try blocks on some, but not all function bodies would make the grammar and compilers more complicated.

有些事情是允许的,因为不允许就更难了。允许在一些函数体上尝试函数块,但不是所有的函数体都会使语法和编译器更加复杂。

#3


3  

Just spotted an interesting point in this Dr. Dobb's article (although quite old):

刚刚在Dobb博士的文章中发现了一个有趣的点(虽然已经很老了):

...remember that you can't return a value inside a function-try-block handler. So it makes no sense to use a function try block for a non-void function

…请记住,不能在函数-try-block处理程序中返回值。因此,对于非虚函数使用函数try块是没有意义的

and this is their code example:

这是他们的代码示例:

int f()
try
{
   ...
}
catch(Error &e)
{
   // oops, can't return int from here!
}

Which actually means that function try blocks are weaker than "regular" try blocks and their use should be discouraged other than in constructors.

这实际上意味着函数try块比常规的try块要弱,除了构造函数之外,不应该使用它们。

(the article is from 2000, so it'd be nice if someone would comment on whether this is still so in the current standard)

(这篇文章是2000年的,如果有人能评论一下这篇文章在目前的标准下是否仍然如此,那就太好了)

#4


1  

Function try blocks were added expressly for the purpose of catching exceptions in constructor initialization lists.

函数try块是为了捕获构造函数初始化列表中的异常而特意添加的。

In your example there are no constructor initializations, so there is no difference between the two forms.

在您的示例中没有构造函数初始化,因此这两种形式之间没有区别。