www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D exceptions and calling c++ code

reply "J" <noname notavailable.notavailabe.com> writes:
Is there anything that would prevent D from catching C++ 
exceptions?

It would be nice to be able to leverage C++ libraries, but if you 
can't catch C++ exceptions, I'm not sure how realistic that is.

That doesn't mean that catch() in D has to be the catcher.

If there was some way to indicate that a function call was into a 
C++ library, and perhaps some way to auto-generate, at the call 
point, the boilerplate that does the "catch this C++ exception 
and re-throw it as a D exception" translation, that would be 
awesome.

Possible?
Mar 29 2013
next sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
this might be relevant: see code that Swig generates when writing C++ wrappers :

eg:
----
// Support for throwing D exceptions from C/C++.
typedef enum {
  SWIG_DException = 0,
  SWIG_DIllegalArgumentException,
  SWIG_DIllegalElementException,
  SWIG_DIOException,
  SWIG_DNoSuchElementException,
} SWIG_DExceptionCodes;

typedef void (* SWIG_DExceptionCallback_t)(const char *);
----

On Fri, Mar 29, 2013 at 10:06 PM, J <noname notavailable.notavailabe.com> wrote:
 Is there anything that would prevent D from catching C++ exceptions?

 It would be nice to be able to leverage C++ libraries, but if you can't
 catch C++ exceptions, I'm not sure how realistic that is.

 That doesn't mean that catch() in D has to be the catcher.

 If there was some way to indicate that a function call was into a C++
 library, and perhaps some way to auto-generate, at the call point, the
 boilerplate that does the "catch this C++ exception and re-throw it as a D
 exception" translation, that would be awesome.

 Possible?

Mar 30 2013
prev sibling next sibling parent Michel Fortin <michel.fortin michelf.ca> writes:
On 2013-03-30 05:06:31 +0000, "J" <noname notavailable.notavailabe.com> said:

 Is there anything that would prevent D from catching C++ exceptions?
 
 It would be nice to be able to leverage C++ libraries, but if you can't 
 catch C++ exceptions, I'm not sure how realistic that is.
 
 That doesn't mean that catch() in D has to be the catcher.
 
 If there was some way to indicate that a function call was into a C++ 
 library, and perhaps some way to auto-generate, at the call point, the 
 boilerplate that does the "catch this C++ exception and re-throw it as 
 a D exception" translation, that would be awesome.
 
 Possible?

In my D/Objective-C hack I made a few years ago, I made it work for Objective-C exceptions [1]. So it's certainly doable. [1]: http://michelf.ca/projects/d-objc/syntax/#exceptions It would be more tricky though because C++ exceptions are thrown by-value. And some things will have to work differently to accommodate each platform's ABI. And you'll likely get a performance penalty in normal code for settings the handler frames. But it can be done. -- Michel Fortin michel.fortin michelf.ca http://michelf.ca/
Mar 30 2013
prev sibling parent "Rob T" <alanb ucora.com> writes:
There was a discussion about Exceptions in D.learn that may be 
relevant.

http://forum.dlang.org/thread/yqzjldpknloyxwlbuxlk forum.dlang.org

If you look though the discussion towards the end you'll see 
mention of "Lippincott functions", and from there a C++ exception 
handler example is shown where you can catch any exception in 
C++. With an exception handler it becomes practical to convert 
any C++ exception into something that D will understand through a 
wrapper function that is exported to D. You won't of course be 
catching the C++ exceptions directly inside D (and you will never 
want to anyway), but you should be able to convert them into 
corresponding D exceptions and catch them from inside your D code.

If you have any success with this, please share your experience.

--rt
Mar 30 2013