Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-09-27 17:33:20


From: "Hillel Y. Sims" <hsims_at_[hidden]>
> "David Abrahams" <dave_at_[hidden]> wrote in message
> news:050a01c26642$032f6e70$6501a8c0_at_boostconsulting.com...
> >

> {
> vector1.push_back(e1);
> try {
> vector2.push_back(e2):
> }
> catch(...) {
> vector1.pop_back();
> throw;
> }
> }
>
> vs.
>
> {
> vector1.push_back(e1);
> ScopeGuard guard =
> MakeObjScopeGuard(vector1,{typeof(vector1)}::pop_back);
> vector2.push_back(e2);
> guard.Dismiss();
> }
>
> The ScopeGuard version is 1) fewer lines of code,

But denser.

> 2) easier to test -- if
> you forget to Dismiss(), you'll find out immediately during ordinary
> testing, vs. missing the throw; in more difficult to test (possibly even
> untested?) exception-handling code,

That's a good and important point. Most people never test their
error-handling paths.

> 3) scales better (what if you have
> three/four/etc. updates to synchronize?),

Yep.

> 4) "not-really-C++" all you like,
> but still exists as a real-world issue,

Sure.

> especially if you are trying to
> write safe portable code (besides, they could be implemented as
"really-C++"
> exceptions on a given platform too).

In that case, try/catch would be fine.

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave_at_[hidden] * http://www.boost-consulting.com


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