This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
g++: Exception handling in constructor
- From: Frank Grimm <fgr at foobar-cpa dot de>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 10 Dec 2002 12:23:53 +0100
- Subject: g++: Exception handling in constructor
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