RFC: reference counted Throwable

Johannes Pfau via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 21 03:26:09 PDT 2014


Am Sun, 21 Sep 2014 09:35:40 +0000
schrieb "deadalnix" <deadalnix at gmail.com>:

> On Sunday, 21 September 2014 at 09:01:45 UTC, Johannes Pfau wrote:
> > +1, replace it completely with malloc/free.
> >
> > However, for backwards compatibility malloced exceptions 
> > probably still
> > have to be added as roots to the GC, at least if they refer GC
> > allocated data. This should be somehow optional however.
> 
> Yeah, so the GC do not scan in it, and the error message 
> disappear randomly under your feet and you segfault when trying 
> to read. Looks like a great idea.

If you always need to add exceptions to GC roots the whole proposal of
reference counting exceptions is useless as you depend on the GC
exactly in the same way as if you allocate from the GC. Andrei initially
proposed pure reference-counted exceptions in -nogc which would suffer
from the same issue.

And storing GC-allocated messages in exceptions is in most cases bad
design. If the message is a string literal there's no GC involved and
if you add 'information' to the message, making it dynamic, then:
* In order to process that information you need to extract it from the
  message string
* You create a dynamic string which might never be used (exception can
  be caught and ignored, etc.)

The correct design is to add the information to a variable in a
subclassed exception and produce the message in toString, which can
work entirely without a GC.

Example:
BAD:
throw new Exception("Timout occured, timeout = " ~
to!string(timeout))
GOOD: throw new TimeoutException("Timeout occured", timout)


More information about the Digitalmars-d mailing list