Article View: comp.std.c++
Article #2629Exceptions in derived class destructors
From: Scott Meyers
Date: Sat, 18 Nov 1995 03:49
Date: Sat, 18 Nov 1995 03:49
50 lines
1809 bytes
1809 bytes
I posted this a while ago as part of a larger posting, but nobody ever
responded. I'm reposting it as a separate question in hope of gaining
insight into the proper behavior of destructors when exceptions are thrown:
If a derived class destructor throws an exception, do member and base
class destructors still get called? Consider this program:
#include <iostream.h>
class M {
public:
~M() { cout << "M destructor\n"; }
};
class B {
M m;
public:
~B() { cout << "B destructor\n"; }
};
class D: public B {
M m;
public:
~D() { cout << "Starting D destructor\n";
throw "Ouch!";
cout << "Ending D destructor\n";
}
};
int main()
{
D d;
return 0;
}
When d is destroyed, D's destructor throws. Are destructors
nevertheless called for d.D::m, the B part of d, and d.B::m? My feeling is
the answer should be no, in the same way that the remainder of D's
destructor is not executed after the throw. Different compilers do
different things, however. Who knows what is supposed to happen?
Scott
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<smeyersDI7uqr.9vt@netcom.com>Path:
arc1-ca.pugleaf.net!news.mindlink.net!agate!agate!MattShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.mindlink.net!agate!agate!Matt From: smeyers@netcom.com (Scott Meyers) Newsgroups: comp.std.c++ Subject: Exceptions in derived class destructors Date: 18 Nov 1995 03:49:06 GMT Organization: NETCOM On-line Communication Services (408 261-4700 guest) Approved: Matt@Austern Message-ID: <smeyersDI7uqr.9vt@netcom.com> NNTP-Posting-Host: austern.hip.berkeley.edu