Catching C++ std::exception in D

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 11 22:50:33 PST 2015


In order to interoperate with modern C++, it has been abundantly clear for some 
time that D needs some support for C++ exception handling:

1. Have D finally blocks executed in D code that sits between a C++ try and catch
2. Have C++ finally blocks executed in C++ code that sits between a D try and catch
3. Be able to catch in D code an std::exception* or a C++ class derived from that.
4. Throw an std::exception* from D code.

That's the minimum credible support, and is likely all D will actually need.

It's also clear that over the years nobody has risen to the challenge to get 
this working in dmd, so it falls to me to do it:

https://www.youtube.com/watch?feature=player_detailpage&v=Nglh-BExEus#t=227

:-)


The Current State
-----------------

Win32: DMC++ and DMD use standard Windows SEH, so this should be fairly easy to 
get to work. Unfortunately, Win32 is the past.

Win64: VC++ uses some bizarre unique scheme. I have no idea how it works. DMD 
uses the DM scheme I invented 30 years ago (!) that is very simple and works 
great, though it does have some performance penalties in the non-EH path.

Linux, OSX, FreeBSD: g++/clang++ use the dwarf eh scheme. DMD uses the DM scheme.


Action Plan
-----------

Switch to using dwarf eh on Linux 64 (our most important platform). Once that 
works, it can be easily (hopefully) extended to the other dwarf eh platforms. 
Eventually, figure out the V++ Win64 scheme.


Progress
--------

I have started by switching from generating .debug_frame sections to .eh_frame 
sections. It appears that gcc/g++ already do this, and .debug_frame is now obsolete.

I'll pretty much have to generate .gcc_except_table myself, as well as redo how 
the finally and catch block code is generated.


Can Really Use Help
-------------------

I really appreciate the offers, and here's what will really help:

Writing the druntime end of things, which is really providing just two 
functions: d_throw (d_dwarf_throw to distinguish it) and the libunwind 
personality function: __dmd_personality_v0. The tricky part with the personality 
function will likely be recognizing std::exception* exceptions. I wonder if 
forwarding the call to __gxx_personality_v0 will work.

Work on this should be independent of the dmd compiler changes.


More information about the Digitalmars-d mailing list