[C++-sig] Letting exception through

Nikolay Mladenov nickm at sitius.com
Thu Jul 7 22:27:31 CEST 2005


Hi David,

David Abrahams wrote:
> 
> Nikolay Mladenov <nickm at sitius.com> writes:
> 
> > Hi,
> >
> > I have a python interpreter running as a plugin to at third party
> > software, and I use boost.python to expose some functionality of
> > this software to python.
> 
> Let's see:
> 
>   3rd-party app                           <----------+
>     calls Python interpreter in your plugin          |
>       which calls a Boost.Python extension module ---+
>          (which calls back into the 3rd party app to expose its
>           functionality)
> 
> ??

That is in principle correct, but because the exposed functionality
includes
UI, there are some additional callbacks crossing the boundary back and
forth.


> 
> > I am having the following problem. At times they (the third party
> > software) throw std::exception which they apparently expect to
> > catch,
> 
> > because when boost.python catches it on the boundary between
> > python and C++, the software crashes.
> 
> You conclude that they expect to catch the exception because the
> application crashes at the moment that Boost.Python catches the
> exception?  That conclusion seems highly suspect.  In general any
> intermediate library should be able to catch an exception and rethrow
> it without the 3rd party app being any the wiser, so if the crash is
> really happening at the catch point, something else is wrong.



I changed the errors.cpp / handle_exception catch block like that:
	..............
           PyErr_SetString(PyExc_IndexError, x.what());
	}
        catch(const std::exception& x)
        {
		bool rethrow = false;
		if(rethrow)
			throw;
                PyErr_SetString(PyExc_RuntimeError, x.what());
        }
        catch(...)
        {
             PyErr_SetString(PyExc_RuntimeError, "unidentifiable C++
exception");
        ...........

and from the debugger changed the rethrow to true when this particular
exception was handled.
They didn't crash. And it seemed the exception went through python
without a problem?

> 
> > So my question is is there a way to have certain exceptions flow
> > through the boost.python boundary?
> 
> Maybe you could do it by registering a special exception
> translator...

I have one big problem - the only thing I now about this exception is,
that it is derived from
std::exception, so translator does not seem like an option. 


> 
> ... but don't do it; it would be very, very wrong!  It will
> certainly crash Python if a C++ exception propagates into it.  What
> you need to do, if my picture is right, is translate the exception
> into something that Python can propagate (e.g. by serializing it), and
> then ressurect the exception (e.g. by deserializing it) in your
> plugin, and throw that.
> 


And I have the same problem here. How can I serialize it without knowing
it ?


Isn't it always appropriate to rethrow exceptions not coming from your
code?
And following this isn't it better to leave the std::exception out of
the 
handle_exception( one can always register translator) ?




Thanks and regards,

Nikolay




More information about the Cplusplus-sig mailing list