scope attribute and catching exception

Max Samukha samukha at voliacable.com.removethis
Mon Oct 13 03:03:34 PDT 2008


On Mon, 13 Oct 2008 05:50:46 -0400, Paolo Invernizzi
<arathorn at fastwebnet.it> wrote:

>Hi all,
>Someone can explain me why the scope attribute of r2 is not satisfied? It's a bug or an expected behaviour?
>
>Paolo
>
>module tests.d.scopes.t02.test;
>
>class Resource {
>    static int allocated = 0;
>    this( bool mustThrow = false ){
>        allocated ++;
>        if( mustThrow ) throw new Exception("bang");
>    }
>    ~this(){
>        allocated --;
>    }
>}
>
>void main(){
>    
>    
>    // Why the destructor of r2 is not called when exiting the scope?
>    {
>        try {
>            scope r1 = new Resource();
>            scope r2 = new Resource(true);
>        }
>        catch(Exception e){}
>    }
>    //assert( Resource.allocated == 0); // Expected
>    assert( Resource.allocated == 1 ); // What's happening
>    
>}

because r2 wasn't successfully created and there is nothing to call
the destructor on:

class Resource {
    static int allocated = 0;
    this( bool mustThrow = false )
   {  
        scope(success)       
            allocated++;
        if( mustThrow ) throw new Exception("bang");
    }
    ~this(){
        allocated --;
    }
}



More information about the Digitalmars-d-learn mailing list