Take the following code;
void DoThrow( const std::exception& e )
{
throw e;
}
int main( int nArgs, char* args[] )
{
std::exception e;
try
{
DoThrow( e );
}
catch( std::exception& e )
{
// const exception ref is caught
}
return 0;
}
I'm trying to retrofit const correctness in my project and inadvertently created the above situation. As it stands, in Dev Studio the catch block DOES catch the exception, despite the fact that it is thrown as a const & but caught as a non-const &.
Question - Should it? :-)
throw
takes an expression and creates via copy-initialization an exception object based on the static type of that expression. The exception object is not a const
object.
The catch
statement initializes a reference to the exception object, not the object (if any) referred to by the throw
expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With