scope() statements and return

Ola Fosheim Grostad via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 3 20:46:35 PDT 2014


On Saturday, 4 October 2014 at 03:21:23 UTC, ketmar via 
Digitalmars-d wrote:
>   scope(exit) {
>     some-cleanup-code
>     thisCanThrow();
>     some-more-cleanup-code
>   }
>
> and what we should do with "some-more-cleanup-code" if 
> "thisCanThrow"
> throws?

It is tricky if the throw implies that the caller to the cleanup 
should retry because of a timeout/deadlock, so the programmer 
have to analyze it and catch it if a retry is implied. So you 
need to catch in scope(exit) and check the coverage during 
semantic analysis.

The alternatives are:
1. formally forbid throwing in all functions that closes/frees 
resources and use return values instead, but that is inconsistent 
with having exceptions in the first place...

2. not free resources, but use a manager object that frees them 
in the background, but that defeats having scope(exit)

3. remove exceptions from the language.

Point 3 sounds nice. :-)


More information about the Digitalmars-d mailing list