C++/D interface: exceptions

via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 12 10:57:39 PDT 2014


On Friday, 12 September 2014 at 16:37:43 UTC, H. S. Teoh via 
Digitalmars-d wrote:
> On Fri, Sep 12, 2014 at 06:19:54PM +0200, Marco Leise via 
> Digitalmars-d wrote:
>> Am Fri, 12 Sep 2014 15:55:37 +0000
>> schrieb "Sean Kelly" <sean at invisibleduck.org>:
>> 
>> > On Friday, 12 September 2014 at 06:56:29 UTC, Jacob Carlborg 
>> > wrote:
>> > > On 64bit Objective-C can catch C++ exceptions. But I don't 
>> > > think you can do anything with the exception, i.e. it uses 
>> > > the following catch syntax:
>> > >
>> > > @catch(...) {}
>> > >
>> > > Would that be easier?
>> > 
>> > I think the trick is setting up the stack frame in such a 
>> > way that the C++ exception mechanism knows there's a catch 
>> > block available at all.  From there, we should be able to 
>> > use the standard interface-to-class method to call virtual 
>> > functions on the exception object, and hopefully the C++ 
>> > runtime will handle cleanup for us.
>> 
>> What exception object?
>> 
>> throw "bad things happened";
> [...]
>
> Yeah, in C++, you can throw *anything*. Including ridiculous 
> things like
> `throw NULL;` or `throw 3.14159;`. There's no method for that! 
> What we
> might end up doing, might be to wrap the C++ exception in a D 
> exception
> that contains a pointer to the C++ type along with whatever 
> type info we
> can glean from the C++ runtime. We probably won't be able to do 
> much
> more than that.

How about

     try {
         my_cpp_func();
     } catch(CppException!(const(char)*) e) {
         writeln(e.payload.fromStringz());
     }

?

Btw, how does implicit conversion work with `catch` in C++? I.e., 
if you throw a `char*`, will it be caught when you catch `const 
char*`? This can not be handled easily with such a template, as 
we would need to catch both `CppException!(const(char)*)` and 
`CppException!(char*)`.


More information about the Digitalmars-d mailing list