scope guards

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 8 05:22:46 PDT 2014


On Friday, 8 August 2014 at 08:37:37 UTC, Messenger wrote:
> On Friday, 8 August 2014 at 00:27:21 UTC, Jonathan M Davis 
> wrote:
>> On Thursday, 7 August 2014 at 20:59:45 UTC, Messenger wrote:
>>> On Thursday, 7 August 2014 at 20:54:02 UTC, Jonathan M Davis 
>>> wrote:
>>>> 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.
>>>
>>> But it's super convenient.
>>
>> It fundamentally doesn't work to handle an exception with a 
>> scope guard. They rethrow the exception.
>
> void fun() {
>     scope(failure) return;
>     throw new Exception(__FUNCTION__);
> }
>
> void main() {
>     import std.exception;
>     assertNotThrown(fun());
> }

Initially I thought this was a bug, but this seems intended since 
it doesn't work with `scope(exit)` - it yields the compiler error 
"Error: return statements cannot be in finally, scope(exit) or 
scope(success) bodies".


More information about the Digitalmars-d mailing list