This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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
+


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]