@nogc exceptions?

Nicholas Wilson iamthewilsonator at hotmail.com
Sun May 19 04:51:07 UTC 2019


On Sunday, 19 May 2019 at 04:24:39 UTC, Manu wrote:
> What's the story with @nogc exceptions?
> There was DIP1008 or whatever, I didn't follow it because I 
> never use
> exceptions... but I kinda need it.

It used to not work, but it does now.

> IIRC, the idea was the throw statement

Specifically it works with `throw new [My]Exception(args);` and 
only that form (since otherwise you could leak it).

> transfer ownership of the exception to the runtime, and then 
> use a `scope` catch block, such that the runtime can be sure it 
> owns the only reference

Well 	
     try throw new Exception("foo");
     catch(scope Exception ex) {}

fails with

Error: basic type expected, not scope
Error: found scope when expecting )
Error: semicolon expected, not )
Error: found ) instead of statement
Error: unrecognized declaration

but I think that was done for compatibility with other code that 
could store the exception.
But regardless

     try throw new Exception("foo");
     catch(Exception ex) {}

will allocate a reference counted exception with malloc(?) (you 
can probably edit druntime to make it use some other scheme) 
which will deallocate at the end of the scope.

> at the end of the catch block and it can free the exception 
> rather than transfer it to the GC?

> How did it fail?

It used to not actually do the thing it was supposed to do (as in 
it would still call the GC). It was fixed.


More information about the Digitalmars-d mailing list