[OT] - C++ exceptions are becoming more and more problematic

rikki cattermole rikki at cattermole.co.nz
Wed Feb 23 16:11:45 UTC 2022


I'm kinda in agreement with Herb's proposal for C++ but for D.

Runtime exceptions are expensive and not worth it for _alternative 
results_ which is where we kinda need them and without them things get 
messy...

As a result this is kinda something I would to have in D. The sort of 
thing I was thinking for D:


Support struct based exceptions (value type), passed on stack (and hence 
RC support) as part of the return value of a function.

Add a new attribute ``throws``. It should include types that say what 
will be thrown.

``@throws(Exception)`` would be equivalent to what D functions without 
``nothrow`` are today by default. ``@throws()`` would be equivalent to 
``nothrow``.

``@throws(Struct)`` would only allow the Struct to be thrown from a 
function.

A function must either explicitly include or inherit all functions it 
calls throwable types. Unless it catches them (for classes that would 
include parents i.e. Throwable would remove all of them).


I would like to hear other opinions especially Walter's since I don't 
think he has commented on value type exceptions design-wise recently.


More information about the Digitalmars-d mailing list