scope guards

Sean Kelly via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 4 16:10:52 PDT 2014


On Sunday, 3 August 2014 at 15:28:43 UTC, Manu via Digitalmars-d
wrote:
>
> I'm thinking this would make all the difference:
>
> scope(failure, MyException e) // only executed for exceptions 
> of type
> MyException
> {
>   writeln(e.msg); // can refer to the exception in this failure 
> block
>   return failureValue; // and can gracefully return from the 
> function too
> }
>
> That would eliminate about 80% of my try/catch blocks.
> The remaining suffer from the problem where I want to respond 
> to exceptions
> NOT of a specific type, ie, clean up in the case of an 
> unexpected/unknown
> exception.
>
> scope(failure, ~MyException)
> {
>   // clean up, because some unexpected exception occurred that 
> I don't/can't handle.
> }

The easiest thing would be to provide a thread-local reference to
the currently in-flight exception.  Then you could do whatever
checking you wanted to inside the scope block.


More information about the Digitalmars-d mailing list