C++ 重新抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6185957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:39:19  来源:igfitidea点击:

Rethrowing exceptions

c++exception

提问by user753213

Why doesn't the following doesn't handle the exception that was rethrown? I tried all the combinations but none of them would show the output in last catch so I'm confused!

为什么以下不处理重新抛出的异常?我尝试了所有组合,但没有一个会显示最后一次捕获的输出,所以我很困惑!

Derived D;

try {
       throw D;
} catch ( const Derived &d) {
       throw;
} catch (const Base &b) {
      cout << "caught!" << endl;
}


Derived D;

try {
    throw D;
} catch ( const Derived d) {
    throw;
} catch (const Base b) {
    cout << "caught!" << endl;
}


Derived D;

try {
    throw D;
} catch ( const Derived d) {
    throw;
} catch (const Base &b) {
    cout << "caught!" << endl;
}


Derived D;

try {
    throw D;
} catch ( const Derived &d) {
    throw;
} catch (const Base b) {
    cout << "caught!" << endl;
}

回答by Lightness Races in Orbit

The re-throw is not handled by the same try-catchblock. It's thrown up to the calling scope.

重新抛出不是由同一个try-catch块处理的。它被抛出到调用范围。

In [except.throw](2003 wording):

[except.throw](2003 年的措辞)中:

A throw-expression with no operand rethrows the exception being handled.

没有操作数的 throw 表达式重新抛出正在处理的异常。

and:

和:

When an exception is thrown, control is transferred to the nearest handler with a matching type (15.3); “nearest” means the handler for which the compound-statement, ctor-initializer, or function-body following the try keyword was most recently entered by the thread of control and not yet exited.

当抛出异常时,控制被转移到最近的具有匹配类型的处理程序(15.3);“nearest” 表示紧跟 try 关键字的复合语句、构造函数初始化程序或函数体最近由控制线程进入但尚未退出的处理程序

Your tryblock has exited, so its handlers are not candidates. Thus, none of the catchblocks in your code may handle the re-throw.

您的try块已退出,因此其处理程序不是候选对象。因此,catch您的代码中的任何块都不能处理重新抛出。

Admittedly this is rather confusing wording.

诚然,这是相当令人困惑的措辞。

回答by Naveen

Rethrown exception is supposed to be caught by some other try..catchblock, not the catch handler of the same tryblock. See this example:

重新抛出的异常应该被其他某个try..catch块捕获,而不是同try一块的 catch 处理程序。看这个例子:

using namespace std;
class Base
{
public:
    virtual ~Base(){}
};

class Derived : public Base
{
};

void f()
{
    try
    {
        throw Derived();
    }
    catch(Derived& ex)
    {
        cout<<"Caught in f\n";
        throw;
    }

}

int main()
{
    try
    {
        f();
    }
    catch(Base& b)
    {
        cout<<"Caught in main\n";
    }

    return 0;
}

output is:

输出是:

Caught in f

Caught in main

夹在 f

主要被抓

回答by B?ови?

This should work :

这应该工作:

Derived D;


try{

    try {
        throw D;
    } catch ( const Derived &d) {
        throw;
    } catch (const Base &b) {
        cout << "caught!" << endl;
    }

} catch (const Base &b) {
    cout << "caught here!" << endl;
}

As other said, the rethrowwill rethrow the same exception out of the catch block.

正如其他人所说,rethrow将从 catch 块中重新抛出相同的异常。