This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
exceptions and ctor initializers
- To: egcs at cygnus dot com
- Subject: exceptions and ctor initializers
- From: Ken Teh <kmt at slowpoke dot phy dot anl dot gov>
- Date: Wed, 11 Nov 1998 11:17:16 -0600 (CST)
I'm using C++ exceptions in egcs-1.1b on a i586-pc-linux-gnulibc1 box. I am
confused by its behaviour when it comes to throwing exceptions in a ctor
initializer list. It doesn't jive with what I've read in Stroustrup's 3rd
edition. Can someone please clarify?
I am trying to put together a simple traceback using exceptions. First, I
define my exception class as follows:
class X {
string _what;
public:
X(const string& what_arg) : _what(what_arg) {}
virtual const char * what () const { return _what.c_str(); }
virtual void append(const string& a) { _what.append(a); }
};
The base and derived classes look like:
class B {
public:
B() throw(X);
virtual ~B() throw() {}
};
B::B() throw(X) {
char m[80];
sprintf(m, "\n%31s:%-8d", __FILE__, __LINE__);
throw X(m);
}
class D : public B {
public:
D() throw(X);
~D() throw() {}
};
D::D() throw(X)
try : B()
{
// do D initialization
}
catch (X& x) {
char m[80];
sprintf(m, "\n%31s:%-8d", __FILE__, __LINE__);
x.append(m);
throw;
}
int main()
{
try { D d; }
catch (X &x) {
fprintf(stderr, "%s\n", x.what());
}
}
When I run this, I expect to see a simple traceback of the exceptions
thrown, that is, 2 lines of traceback showing each point at which the
exception was thrown (and rethrown).
I don't. I see only one line and that's the line thrown by B's constructor.
The D constructor catch block is not invoked. Why? There is no real
concern; I still have sufficient information on hand, and if the stack
unwind cleans up properly, then all is well. But, I'm still confused.