g++: Exception handling in constructor

Frank Grimm fgr@foobar-cpa.de
Tue Dec 10 03:23:00 GMT 2002


Hi,

I'am woundering why the following code does not work/run correctly with
g++-3.* 

///////////////////////////

#include <iostream>

using std::cout;
using std::cerr;
using std::endl;

struct Exception { };

struct Bad {
    Bad(int) { cout<<"Throwing Exception..."<<endl; throw Exception(); }
};

struct Test {
    Bad b;

    Test() try : b(Bad(0)) {
        cout<<"Test"<<endl;
    } catch(Exception) {
        cerr<<"Caught Exception -- this is right"<<endl;
    }
};

int main() try {
    Test t;
} catch(Exception) {
    cerr<<"Caught Exception -- should not happen"<<endl;
};

///////////////////////////

The output looks like this:

Throwing Exception...
Caught Exception -- this is right
Caught Exception -- should not happen

Why is the exception re-thrown to the creator of Test t (main function)?

IMHO the C++ standard says that an exception can a processed in the
constructors catch block, but i did not find anything about re-throwing
the exception to the caller of the constructor.?

I didn't find anything in the gcc bug database about exception handling
in constructors and initializer lists concerning this problem ... 

Thanks, Frank. 
-- 
Frank Grimm // fgr@foobar-cpa.de
            // www.foobar-cpa.de



More information about the Gcc-help mailing list