Use C++ exception model in D

Brad Roberts braddr at puremagic.com
Tue Apr 8 11:53:41 PDT 2014


On 4/8/14, 5:03 AM, David Nadlinger wrote:
> 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

I think, for a mixed language application, that the important part is proper object lifetime 
management more than being able to catch exceptions from different languages.  When unwinding a c++ 
exception that has stack frames intermixed with D, destructors need to be executed appropriately, 
and vice versa.

This is also an important part of having abi compatibility between D compilers, something which we 
don't have today but really must have eventually.. hopefully not years away.


More information about the Digitalmars-d mailing list