Use C++ exception model in D

David Nadlinger code at klickverbot.at
Tue Apr 8 05:03:21 PDT 2014


On Tuesday, 8 April 2014 at 10:08:24 UTC, Jacob Carlborg wrote:
> Is there a reason to not use the same model, or what's required 
> to be compatible?

In short, the reason not to use the same "model" (you could argue 
that the model is the same, as only the personality functions 
differ) is that the exception unwinder is intimately tied to both 
the target language ABI and semantics.

For example, all unwinders need to handle the run-time type 
information for the exception object to correctly dispatch it. 
And while e.g. the GNU C++ unwinding implementation needs to pull 
shenanigans to implement the crazy C++ exception lifetime model 
(cf. catching by reference vs. by value) and enforce throws(...) 
specifications, the D unwinder needs to understand the difference 
between Exception and Error and correctly implement exception 
chaining.

Now, of course, it is possible to find a middle ground that works 
as a basis for virtually all languages. In fact, I'd argue that 
SEH for Windows actually provides such a layer, and libunwind 
does too. For example, a proper implementation of a libunwind 
personality function allows you to check whether a given 
exception originated in your own language, and just pass it on 
otherwise (such that e.g. C++ exception just bubble through a 
layer of D code). In theory, it would e.g. also be possible to 
wrap foreign exceptions in e.g. D Throwable objects to make them 
catchable in D code. But the lifetime of the foreign object is 
virtually impossible to get right in the general case, and the 
benefits of wrapping exceptions like this have turned out not to 
be worth it, because it is hard to handle them in a sensible way 
in the receiving language (DMD actually does – did? – something 
like this on Win32).

David


More information about the Digitalmars-d mailing list