scope attribute and catching exception

Denis Koroskin 2korden at gmail.com
Mon Oct 13 02:54:42 PDT 2008


On Mon, 13 Oct 2008 13: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
> }

Looks like no destructor is called if ctor throws an exception.
Try throwing outside of it and see what happens.


More information about the Digitalmars-d-learn mailing list