Topic: ?s about exceptions in destructors


Author: boukanov@sentef1.fi.uib.no (Igor Boukanov)
Date: 1996/04/02
Raw View
Consider the following:
struct A {
   ~A(){throw int(0);}
};

struct B {
   A a;
   ~B(){throw float(0.0);}
};

void f() { B b; }

So according to the 09.95 DWP, 15.5.1.1 any call to f will call
terminate().

But what should happen in the next code:

void f2() {
   char buf[sizeof(B)];
   B* b = new(buf) B;
   b->B::~B();
};

void g2() {
   try { f2(); } catch(float) { }
}

I suppose that g2 will catch float exception after "throw float(0.0);"
in B::~B and A::~A of b->a will not be called because according to 15.2:
" 1 As  control  passes  from  a throw-point to a handler, destructors are
  invoked for all automatic objects constructed since the try block  was
  entered.
2 An object that is partially constructed will have destructors executed
  only for its fully constructed sub-objects..."

and because b is NOT a automatic object so b->a is NOT a automatic object too.

Is this right?

And what should happen in the next:
void f3() {
   B* b = new B;
   delete b;
};

Is it supposed that this lines can be rewritten as:
void f4() {
   B* b = new B;
   try { b->B::~B(); }
   catch(...) { operator delete(b); throw; }
   operator delete(b);
};

So A::~A() for b->a will not be called again but memory will be released?


--
Regards, Igor Boukanov.
igor.boukanov@fi.uib.no
http://www.fi.uib.no/~boukanov/


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]