C++ Why can this throw?

817 views Asked by At

I've created a small exception class. I want to make a constructor that does not throw, but for some reason, the compiler is telling me that the constructor may throw, despite the "catch all" handler :

invalid_csv::invalid_csv( size_t r, size_t c, const char * msg ) throw()
try :
    std::runtime_error( msg ),
    row( r ),
    col( c ),
    m_init_ok( true )
{
}
catch( ... )
{
    m_init_ok = false;
}

.

warning C4297: 'csvrw::invalid_csv::invalid_csv': function assumed not to throw an exception but does

Why does it ? Thank you.

1

There are 1 answers

3
parik On BEST ANSWER

To resolve C4297, do not attempt to throw exceptions in functions that are declared __declspec(nothrow), noexcept(true) or throw(). Alternatively, remove the noexcept, throw(), or __declspec(nothrow) specification.

Source

A so-called function-try-block like this cannot prevent that an exception will get outside. Consider that the object is never fully constructed since the constructor can't finish execution. The catch-block has to throw something else or the current exception will be rethrown

Read this answer