* Stefan Arentz:
> (Alf P. Steinbach) writes:
>
> ...
>
> > > Mayby someone cann tell me why this was not taken into C++
> >
> > Because 'finally' is rarely needed in C++, and where it is needed it
> > indicates a redesign/refactoring is called for, and in the extremely
> > rare cases where that isn't an option, you can easily emulate it.
>
> So how do you emulate it.
One way: you exit the inner block by throwing an exception so that all exits
are via exceptions. In the common catch handler you clean up (this is the
'finally' part) and then check whether the exception corresponds to a normal
return. If so, you do a normal return, otherwise you rethrow. I think this
should perhaps be tought in programming classes because it makes the cost of
'finally', what goes on behind the scenes, very explicit. On the other hand
there is the risk that what students know about they will use...
Another way: you pass references to things to be cleaned up to an object of a
locally declared class where the destructor implements the 'finally'.
But generally, use smart-pointers, RAII and exception-transparent code to
avoid the need for 'finally' handling: whenever you find yourself thinking
that 'finally' would be nice here, think about how much nicer if 'finally'
weren't needed here at all, i.e. if the code was refactored/redesigned!

)
PS: No need to use gcc extension to define an inner function; where you
need it you can use a local class.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?