On exceptions in D

Jakob Ovrum jakobovrum at gmail.com
Sun Feb 9 10:14:36 PST 2014


On Sunday, 9 February 2014 at 17:57:23 UTC, Dmitry Olshansky 
wrote:
> void risef(T...)(string fmt, T args)
> {
>     static Failure!T slot;
>     if(!slot)
>         slot = new Failure!T();
>     slot.assign(fmt, args);
>     throw slot;
> }

If the template is instantiated we can say with almost certainty 
that the function will be called, so it'd be an improvement to 
allocate the exception instance statically (lazily initialized 
with emplace), instead of statically allocating just a reference 
to a GC-allocated instance.

Also, it's nice to keep the throw statement in user code, e.g. 
`throw allocExf(fmt, args);`, as `throw` is a no-return statement 
like `return`, `assert(false)` etc.


More information about the Digitalmars-d mailing list