|
This post has NOT been accepted by the mailing list yet.
My question is how should one raise an exception in C++ when binding a function to Python using Boost::Python? Mod_Init is called from Python. Suppose your C++ code looks like:
#include "Message.h"
#include "ErrStat.h"
#include "A_Class.h"
#include "MAP_ERROR_CODE.h"
using namespace boost::python;
BOOST_PYTHON_MODULE( Mod ) {
def("Mod_Init", Mod_Init);
class_<Message>( "Message" )
.def( /* define a method here */ );
class_<ErrStat>( "ErrStat" )
.def( /* ... */ );
class_<A_Class>( "A_Class" )
.def( /* ... */ );
};
void A_Class::update( ErrStat &err , Message &msg ) {
/* ... */
try {
this->set_a_value( ); // this function throws a MAP_ERROR_CODE
} catch ( MAP_ERROR_CODE error_code ) {
err.set_error_key( error_code );
msg.write_error_message("Error Message to file");
/* handle exception here */
}
/* ... */
};
void Mod_Init( A_Class &data, ErrStat &errstat, Message &messge ){
/* some code */
data.update( errstat , message ); // this function raises an exception
/* ect */
};
When this snippet of code is run from the command line, the code terminates with RuntimeError: unidentifiable C++ exception. I would rather have the code handle the exception in the catch phrase rather than the program unexpectedly terminating.
On another note, I'm only showing a small portion of the code relevant to Boost::Python. This same program also has Fortran wrappers, and so I'm hoping any modifications I have to make to the above code won't break the Fortran bindings I have constructed.
|