Pregunta

Let :

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

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

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

What does baz() catch ? An std::exception or something else ?

Thanks for your help !

¿Fue útil?

Solución

It catches the same std::exception that was thrown by foo. (At least, it would, if it were possible to throw std::exception like that in the first place.) throw; with no operand rethrows the exception object that's currently being handled.

Otros consejos

Yes, baz catches std::exception in this case.

But be careful when throwing std::exception because it should be used as a base class of exceptions. C++ Standard (Paragraph 18.8.1) specifies that std::exception only has a default constructor and a copy constructor, so you cannot put message into it.

Consider using std::runtime_error instead.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top