Mizux / cmake-swig

Modern CMake C++ SWIG Sample
Apache License 2.0
177 stars 30 forks source link

Exception support #7

Open romainreignier opened 4 years ago

romainreignier commented 4 years ago

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.

Mizux commented 4 years ago

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

romainreignier commented 4 years ago

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.

Mizux commented 4 years ago

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.

romainreignier commented 4 years ago

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 notifications@github.com:

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]

Mizux commented 4 years ago

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

romainreignier commented 4 years ago

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")