views:

136

answers:

1

We've got an interface we've defined in C++ (abstract class, all functions pure virtual) which will be extended in Python. To overcome the cross-language polymorphism issues we're planning on using SWIG directors. I've read how to catch exceptions thrown from C++ code in our Python code here, here, here, and even on SO.

It's fairly straight forward and I'm not expecting issues with handling our library's own exceptions. What I'd like to know and can't seem to find in the documentation is how to have our Python implementation of the extended C++ interface throw those C++ exceptions in a way that makes them visible to the C++ code.

We could make small functions within the *.i files such that each function throws our exceptions:

void throw_myException(){ throw MyException; }

but I'm wondering how it will interact with the Python code.

Anyone have any experience with throwing C++ exceptions from Python code?

+2  A: 

(C)Python is written in C. It seems that it could be bad to throw exceptions "through" the interpreter.

My feeling is that it's probably safest to return a token of some sort from your API that can create an exception via a factory.

That's basically what we do here, although we're using C# instead of Python to generate the "error code" data which then gets translated to the C++ layer and then sent off to the exception factory.

dash-tom-bang
We were trying a variety of things here but finally have settled on using this method. It's the most clear and efficient way suggested by anyone. Thanks much.
wheaties