简体   繁体   English

非构造函数上的函数try块是否有任何缺点?

[英]Do function try blocks on non-contructor functions have any disadvantage?

Function try blocks are a special form of function bodies, for example: 函数try块是函数体的一种特殊形式,例如:

int f() try {
  // function body
} 
catch {
  // one or more catch-clauses.
}

The primary purpose is for usage in constructors, in order to log exceptions thrown by the constructor of any base class. 主要目的是在构造函数中使用,以便记录任何基类的构造函数抛出的异常。 However, it is allowed to use them in regular functions, too. 但是,也可以在常规函数中使用它们。

There exist some (quite old) questions on this, asking why would we need it for regular functions, eg Function try blocks, but not in constructors . 对此存在一些(相当古老的)问题,询问为什么我们对于常规函数(例如, 函数try块)需要它,但在构造函数中却没有 However, my question is more in slightly in another direction: Can I use it in regular functions as replacement for a regular try-block without concerns? 但是,我的问题更多地指向另一个方向:我可以在常规函数中使用它代替常规try-block而不用担心吗? Let's say, just for aesthetical reasons? 假设是出于审美原因?

I develop a C-Interface for a C++-Library and need to encapsulate each interface-function with a try-block to catch any exceptions. 我为C ++库开发了C接口,并且需要用try块封装每个接口功能,以捕获任何异常。 Thus, I would like to avoid an additional curly-bracket-block in each function... 因此,我想避免在每个函数中使用额外的花括号。

Just one thing, which raised my concerns: In the answer https://stackoverflow.com/a/11535436/6695750 , davka cites an article from 2000, claiming that you can't return a value from a catch-block corresponding to a function-try-block. 有一件事引起了我的担忧:在答案https://stackoverflow.com/a/11535436/6695750中 ,davka引用了2000年的一篇文章,声称您不能从与a对应的catch块中返回值。功能试块。 I tested with gcc 5.4.0, there I can return a value from the catch-block without problems. 我使用gcc 5.4.0进行了测试,在那里我可以从catch块中返回一个值而不会出现问题。 Is this standard, or a non-standard extension of gcc? 这是gcc的标准扩展,还是非标准的扩展?

int f() try {
  // function body
} 
catch (/*..*/){
  // one or more catch-clauses.
}

is equivalent to 相当于

int f() {
    try {
      // function body
    } 
    catch (/*..*/){
      // one or more catch-clauses.
    }
}

for regular functions. 用于常规功能。

Only constructor/destructor has special treatment as the catch blocks throw a exception(implicitly or explicitly). 由于catch块会抛出异常(隐式或显式),因此只有构造函数/析构函数具有特殊处理。

see also the docu here . 另请参阅文档

Can I use it in regular functions as replacement for a regular try-block without concerns? 我可以在常规功能中使用它代替常规try-block而不用担心吗?

In some case you CANNOT use function-try-block : You CANNOT access any local variables in the the catch clause. 在某些情况下,您不能使用function-try-block :您不能在catch子句中访问任何局部变量。

void g() {
    int i = 2;
    try {
        throw 2.3;
    } catch (double d) {
        cout << i << endl;   // OK. you can access i
        cout << d << endl;
    }
}

void f() try {
    int i = 2;
    throw 2.3;
} catch (double d) {
    cout << i << endl;   // FAIL! i is out of scope, you CANNOT access it.
    cout << d << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM