scope(exit) without exception handling?

Brad Roberts braddr at puremagic.com
Tue May 15 22:38:58 PDT 2012


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.


More information about the Digitalmars-d mailing list