Boost logo

Boost :

Subject: Re: [boost] D-style scope guards?
From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2011-03-06 05:54:36


Jeffrey Lee Hellrung, Jr. wrote:
> On 3/5/2011 4:24 PM, Lorenzo Caminiti wrote:
> [...]
> >1. SCOPE EXIT
> >
> >First of all, note that Boost.ScopeExit code (and therefore,
> >Boost.Local exit code) is *not* executed when throwing an exception:
> >
> >#include<boost/scope_exit.hpp>
> >#include<iostream>
> >#include<stdexcept>
> >
> >void f() {
> > bool error = false;
> >
> > BOOST_SCOPE_EXIT( (&error) ) {
> > std::cout<< "returning"<< std::endl; // Not executed on throw.
> > } BOOST_SCOPE_EXIT_END
> >
> > throw std::runtime_error("some error");
> >}
> >
> >int main() {
> > f();
> > return 0;
> >}
> >
> >This will *not* print "returning" because the throw terminates `f()`
> >*without* executing local variables' destructors (which execute the
> >scope exit code).
> [...]
>
> Wait...I thought that was the point of Boost.ScopeExit? Aren't the
> destructors of local variables always invoked during stack
> unwinding?

On NetBSD 5.99.47 with gcc 4.1.3:
$ g++ -I /home/alnsn/src/boost/trunk /tmp/x.cpp && ./a.out
terminate called after throwing an instance of 'std::runtime_error'
  what(): some error
Abort (core dumped)

However, if you catch it, you'll see "returning".

Alex


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk