Re: Exceptions as smart pointers
David Abrahams wrote:
class A {
// ...
~A(bool in_stack_unwinding) {
if (in_stack_unwinding) { /* ... */ }
else { /* ... */ }
}
};
That appears to only change the interface, but not address the semantic
issues with uncaught_exception. More specifically:
under what conditions will in_stack_unwinding be true above?
Please refer to 15.2p3: The process of calling destructors for
automatic objects constructed on the path from a try block to a
throw-expression is called "stack unwinding."
I.e. in_stack_unwinding destructor argument is true if the
destructor
was called during the stack unwinding. According to 15.2 if
(in_stack_unwinding) then an exception can leave the destructor without
a terminate().
If you're suggesting semantics equivalent to "is it safe to throw," I
don't _think_ that's actually implementable.
But it IS.
The EH-support runtime calls a destructor with
in_stack_unwinding==true
argument so this destructor does really know whether it's safe to throw
exceptions.
Can you give a summary of what you achieve by throwing smart pointers?
1. Smart pointers are cheap to copy and the throwing/catching
exceptions supposes copying. So you can create arbitrary big Exception
objects, throw them as sh_ptr<Exception>(/* ... */) and they will be
copied almost for free.
2. sh_ptr copy constructor doesn't throw exceptions so there is no
chance to get terminate() during the copying of sh_ptr<Exception>(). On
the contrary, Exception copy constructor does really throw exceptions so
its copying is expensive and can lead to terminate().
3. It's really easy to create the chains of the nested Exception
objects.
4. Having recatchException() function you can use the following
uniform
exception handler in almost all of the places:
void f()
{
try {
// ...
g();
// ...
return;
}
catch (...) {
throw newException(_FLINE_, "Problems in f()",
recatchException(mp, _FLINE_));
}
}
--
With all respect, Sergey. http://ders.stml.net/
mailto : ders at skeptik.net
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]