Catching C++ std::exception in D

Elie Morisse via Digitalmars-d digitalmars-d at puremagic.com
Thu Nov 12 07:49:41 PST 2015


On Thursday, 12 November 2015 at 06:50:31 UTC, Walter Bright 
wrote:
> 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.

I also started working on C++ exception handling for LDC/Calypso, 
I almost caught my first std::exception yesterday! (it went into 
the right catch and the IR looks right but _cxa_begin_catch() 
seems to return null instead of the exception object for some 
reason)

Clang and Calypso made the std::type_info stuff a breeze, getting 
the type_info value for any C++ type was one line of code.

I'll push the changes as is into a branch tonight, it's about 
~400 lines.

> 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.

 From what I gathered even if you manage to make the format of the 
LSDA tables compatible with __gxx_personality_v0, one blocker is 
that it still expects std::type_info pointers in the action table 
so will choke if it encounters D's TypeInfo_Class in there.

To check if a type_info from a catch clause handles the thrown 
exception it calls the virtual method type_info::__do_catch 
(which is what Calypso does too) so can't work with D classes.


More information about the Digitalmars-d mailing list