Search code examples
cython

In Cython, if I don't add "except + " to c++ function declaration , and just do a try / catch on python side for RuntimeErrors, won't that work?


if i declare a c++ function in a pxd file without except + at the end, but still do try/catch on my python calling code, except i catch everything or just catch 'RuntimeError', won't that have same effect as having except + ? According to http://docs.cython.org/en/latest/src/userguide/wrapping_CPlusPlus.html#exceptions, most of C++ exceptions will be converted to RuntimeError anyway even with except +.


Solution

  • No that will not work.

    except + is the indicator that tells Cython to generate the code that converts C++ exceptions to a Python exception. Without it Cython will assume that the function cannot throw a C++ exception and the C++ exception will propagate through the Cython code breaking everything.