Topic: Catching member and base class exceptions


Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/04/11
Raw View
In article GMr@news2.new-york.net, "Stephen Cipolli" <stephen.cipolli@qed.net> writes:
>I've heard that the ratified draft standard added the ability to catch
>exceptions from a a constructors member initialization list.  Does the
>standard also support a similar construct for destructors?  That is,
>the ability to catch exceptions from its member's and base classes
>destructors.

You are thinking of a function-try-block, which goes around the body
of a function. In the case of a constructor, the try-block includes
the ctor-initializers. Any exception that escapes from a ctor-initializer
(which includes base-class constructors) or from the function body
can be caught by a catch-block associated with the function-try-block.
Example:

 A::A()
 try : x(1), y(2), z(3)
 {
  ... // body of ctor
 }
 catch( E1& e1) { ... }
 catch( E2& e2) { ... }

If any of the base-class constructors or other initializers exits via
an E1 or E2 exception, it will be caught by one of the trailing
catch clauses. If the body of A::A exits via an E1 or E2 exception,
it will also be caught by one of the trailing catch clauses.

For a destructor, you could write
 A::~A()
 try
 {
  ... // body of dtor
 }
 catch(E1& e1) { ... }
 catch( E2& e2) { ... }
But remember the base class dtors are not run until the derived class
dtor has completed. You cannot catch an exception thrown from a
base class destructor in a derived class function-try-block.

You can put a function-try-block around any function, but they
were developed to solve the problem of exceptions thrown from
constructor init lists. There is no other way to detect that
they occurred. For other than constructors, you can move the
whole thing inside the body of the function and get the same
effect.

---
Steve Clamage, stephen.clamage@sun.com



[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]