cmake-swig icon indicating copy to clipboard operation
cmake-swig copied to clipboard

Exception support

Open romainreignier opened this issue 5 years ago • 6 comments

Thank you for this nice work!

I was wondering if you ever tried to propagate exceptions from C++ to the other languages? This may be added to this example.

romainreignier avatar Jun 17 '20 14:06 romainreignier

Good question ! To be honest, at Google, C++ exceptions are forbidden so I never dig into this topic, however AFAIK SWIG provide some macros (i.e. tooling) to deal with exceptions forwarding could be fun to give it a try once I have time to work on it ^^.

Mizux avatar Jun 17 '20 16:06 Mizux

Ok, I see.

I have tried it by adding

%include "std_except.i"

And manually adding the catches keyword for method definition:

%catches(std::runtime_error) MyLib::my_method();

It works on Python and map to a RuntimeError.

But I was wondering if there is another method.

romainreignier avatar Jun 17 '20 16:06 romainreignier

According to the doc

Exceptions are automatically handled for methods with an exception specification. Similar handling can be achieved for methods without exception specifications through the %catches feature.

Mizux avatar Jun 17 '20 17:06 Mizux

Yes, but Exception specification is deprecated since C++11 and removed in C++17. So %catches seems like the only choice.

17 juin 2020 19:58:16 Mizux [email protected]:

According to the doc[http://www.swig.org/Doc4.0/SWIGDocumentation.html#SWIGPlus_exception_specifications]

Exceptions are automatically handled for methods with an exception specification. Similar handling can be achieved for methods without exception specifications through the %catches feature.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub[https://github.com/Mizux/cmake-swig/issues/7#issuecomment-645529402], or unsubscribe[https://github.com/notifications/unsubscribe-auth/ACDPSYGVWDDP7U25QFXDRYDRXD73RANCNFSM4OAVPB7A]. [https://github.com/notifications/beacon/ACDPSYDPKDQ2CL7UK3XGKCLRXD73RA5CNFSM4OAVPB7KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEZ476OQ.gif]

romainreignier avatar Jun 17 '20 19:06 romainreignier

looking at the swig code: https://github.com/swig/swig/blob/master/Lib/typemaps/std_except.swg and https://github.com/swig/swig/blob/master/Lib/typemaps/exception.swg

swig should be able to catch most standard exceptions hope to play with it soon !

e.g. adding in C++ Foo class a method:

void letsThrow(int idx) {
  switch (idx) {
    case 1:
      throw std::runtime_error("runtime error");
   case 2: 
   ...
  } 
}

and in foo.i

%include exception.i

 %exception {
    try {
      $action
    }
    SWIG_CATCH_STDEXCEPT // catch std::exception
    catch (...) {
     SWIG_exception_fail(SWIG_UnknownError, "Unknown exception");
    }
  }

%feature("except")

foo::letsThrow(int);

and see what's happen :smile: when called from Python/Java/.Net

Mizux avatar Jun 17 '20 22:06 Mizux

Oh, this snippet seems great! It avoids to declare a %catches statement for each method. To make it work, I had to remove the line:

%feature("except")

romainreignier avatar Jun 18 '20 08:06 romainreignier