Google's Go & Exceptions
    Rainer Deyke 
    rainerd at eldwood.com
       
    Tue Jan 26 16:38:45 PST 2010
    
    
  
Jérôme M. Berger wrote:
> 	Throwing exception inside constructors should be avoided because
> then the destructor is never called and you risk leaking like crazy.
That's not an issue if you properly encapsulate your resources.  One
class, one responsibility.
Consider (C++):
  class C {
    C() { ... }
    Resource a, b, c;
  };
If any of the resource constructors throw, construction of the 'C'
object is aborted and the any Resource objects that have already been
constructed are properly destroyed.  By the time the body of the 'C'
constructor is entered, all the resource already exist and are
guaranteed to be properly disposed of.
If you have a struct/class that performs more than one logical action in
its constructor and/or destructor, refactor it into several structs/classes.
-- 
Rainer Deyke - rainerd at eldwood.com
    
    
More information about the Digitalmars-d
mailing list