Article View: comp.std.c++
Article #2669Re: Exceptions in derived class destructors
From: Fergus Henderson
Date: Tue, 21 Nov 1995 15:46
Date: Tue, 21 Nov 1995 15:46
39 lines
2585 bytes
2585 bytes
smeyers@netcom.com (Scott Meyers) writes: >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? If you are asking about what the current working paper says, I'm not sure off-hand. If you are asking about what the working paper *should* say, then I think the answer should be yes, member and base class destructors should still get called. Otherwise the destructor would have to catch execptions and manually destroy the bases and members before rethrowing to avoid resource leaks. This would be extremely tedious coding, and would be extremely difficult - probably totally impractical - in the case of virtual inheritence. > 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. I disagree. The answer should be yes, in the same way that bases and members are destroyed if an exception occurs in the constructor. Any class which attempts to manage more than one resource must be careful do its own recovery if an exception occurs after constructing or destroying a subset of those resources. But for a class which merely aggregates different resources which are managed by members or base classes, the author of that class should not have to worry about exception handling. -- Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3 --- [ 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:
<9532601.3226@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!cs.mu.OZ.AU!bounce-backReferences:
<smeyersDI7uqr.9vt@netcom.com>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++ Path: arc1-ca.pugleaf.net!cs.mu.OZ.AU!bounce-back From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson) Subject: Re: Exceptions in derived class destructors Message-ID: <9532601.3226@mulga.cs.mu.OZ.AU> Originator: fjh@munta.cs.mu.OZ.AU Sender: news@cs.mu.OZ.AU (CS-Usenet) Organization: Computer Science, University of Melbourne, Australia References: <smeyersDI7uqr.9vt@netcom.com> X-Original-Date: Tue, 21 Nov 1995 14: 01:55 GMT Date: Tue, 21 Nov 1995 15:46:23 GMT Approved: fjh@cs.mu.oz.au X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMLH0XeEDnX0m9pzZAQH5mgF/fykFn8imPZuh2q97sJj7pOLC39q05McQ EUm1kon8SkIVzF+iYNh/Isa05YHHNKNO =Oeev