C ++ Exception重新抛出省略号

时间:2013-11-21 13:54:07

标签: c++ exception try-catch throw

让:

void foo( void )
{
    throw std::exception( "" );
}

void bar( void )
{
    try
    {
        foo():
    }
    catch( ... )
    {
        throw;
    }
}

void baz( void )
{
    try
    {
        bar();
    }
    catch( ... )
    {
    }
}

baz()捕获了什么? std :: exception还是别的什么?

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

它捕获std::exception引发的foo。 (至少,如果有可能首先抛出std::exception,那么。throw;没有操作数会重新抛出当前正在处理的异常对象。

答案 1 :(得分:1)

是的,在这种情况下,baz会抓住std::exception

但是在抛出std::exception时要小心,因为它应该用作异常的基类。 C ++标准(第18.8.1节)指定std::exception只有一个默认构造函数和一个复制构造函数,因此你不能将消息放入其中。

请考虑使用std::runtime_error

相关问题