[C++-sig] Letting exception through
dave at boost-consulting.com
Fri Jul 8 00:37:51 CEST 2005
Nikolay Mladenov <nickm at sitius.com> writes:
> 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;
> PyErr_SetString(PyExc_RuntimeError, x.what());
> PyErr_SetString(PyExc_RuntimeError, "unidentifiable C++
> 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?
That is completely unreliable. 'C' code isn't written with the
expectation that functions might fail to complete because of an
exception. Invariants may be broken temporarily and never restored.
Furthermore the 'C' runtime isn't -- in general -- built to deal with
C++ exceptions and the C++ runtime isn't -- in general -- built to
deal with C stack frames.
If you feel the need to follow that road, be my guest, but I can't
>> ... 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 ?
Yep, you have a problem.
> Isn't it always appropriate to rethrow exceptions not coming from
> your code?
No. And especially not if it means they propagate into code that is
not prepared to deal with exceptions.
> And following this isn't it better to leave the std::exception out
> of the handle_exception( one can always register translator) ?
No. We'd still need catch(...) to stop the exceptions from
propagating into 'C' code.
More information about the Cplusplus-sig