scope guards

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 7 13:54:01 PDT 2014


On Sunday, 3 August 2014 at 15:28:43 UTC, Manu via Digitalmars-d 
wrote:
> I'm trying to make better use of scope guards, but I find 
> myself belting
> out try/catch statements almost everywhere.
> I'm rather disappointed, because scope guards are advertised to 
> offer the
> promise of eliminating try/catch junk throughout your code, and 
> I'm just
> not finding that to be the practical reality.

scope guards are for the occasions when you need to guarantee 
that something happens when either an exception isn't thrown 
(success), is thrown (failure), or regardless of whether one was 
thrown (exit). It's not intended for handling exceptions, just to 
reacting to their existence.

So, it's primarily going to be used when you want to do something 
when an exception was thrown but don't care what was thrown or if 
you want to make sure that something happens regardless of 
whether an exception was thrown (in which case, it's a 
replacement for RAII or a finally block).

I'd say that if you're trying to use scope in any situation where 
you'd try and handle an exception, then you're using it wrong.

- Jonathan M Davis


More information about the Digitalmars-d mailing list