This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
G++ and excemtion
- From: Laurent Marzullo <laurent at marzu dot org>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 5 Jul 2007 16:44:21 +0200
- Subject: G++ and excemtion
Hello,
Here a little program.
#include <iostream>
class ErrorBase {}
class Error : public ErrorBase {}
void
throw_exception( const ErrorBase& excp )
{
throw excp;
}
void
function( void )
{
Error a;
throw_exception( a );
}
int
main( void )
{
try
{
function();
}
catch ( const Error& err )
{
std::cerr << "Error\n";
}
catch ( const ErrorBase& err )
{
std::cerr << "ErrorBase\n";
}
catch ( ... )
{
std::cerr << "Unknown exception !\n";
}
}
This program call a function which throw an exception of type 'Error' (which
inherite from 'ErrorBase').
But the catch bloc only catch ErrorBase exception and not Error ?
Is it a normal behavior ?
I know that there's a copy of the object when a throw occur but is it not a copy
of the real type of the object that must happen ?
I want my function throw_exception to throw the real type of the object passed
as parameter. How may I achieve this ?
May I dynamic_cast to all possible exception that must be passed to
throw_exception as:
if ( dynamic_cast<const Error*>( &excp ) != 0 )
throw Error();
else
throw excp;
Any hint, help ??
Thanks a lot.
Regards
+--------------------+
+ Laurent Marzullo
+