scope(exit) without exception handling?

Jonathan M Davis jmdavisProg at gmx.com
Tue May 15 23:25:55 PDT 2012


On Tuesday, May 15, 2012 22:38:58 Brad Roberts wrote:
> On 5/15/2012 10:06 PM, Jonathan M Davis wrote:
> > On Wednesday, May 16, 2012 05:54:04 Mehrdad wrote:
> >> I'm writing some (low-level) code with no exception handling
> >> available whatsoever... either the code runs, or it doesn't.
> >> 
> >> Is there any way for me to use scope(exit) (or perhaps a
> >> destructor, like RAII) to mean, "Execute this block of code for
> >> me when the block is exited, will ya?", *without* introducing
> >> dependencies on exception handling?
> > 
> > scope(exit) stuff;
> > otherStuff;
> > 
> > is lowered to something like
> > 
> > try
> > {
> > 
> >     otherStuff;
> > 
> > }
> > finally
> > {
> > 
> >     stuff;
> > 
> > }
> > 
> > So, you can use scope(exit) if the above code is acceptable for whatever
> > you're doing. Otherwise, no, you can't.
> > 
> > Destructors should work regardless of what you're doing with exceptions
> > though, so I would expect RAII to work.
> > 
> > - Jonathan M Davis
> 
> And if otherStuff is marked all nothrow, then the exception parts are pulled
> out.  It's pretty much the entire point of having nothrow annotations.

Cool.

- Jonathan M Davis


More information about the Digitalmars-d mailing list