[C++-sig] Re: Custom Exception objects

John A Meinel john at johnmeinel.com
Sat Nov 6 15:51:19 CET 2004


Mike Rovner wrote:
> John A Meinel wrote:
> 
>> I was wondering how to handle custom exception objects using 
>> boost::python.
> 
> 
>> Since I have already wrapped ConfigError as a boost::python class, is 
>> it possible to do something more like:
> 
> 
> suppose you have
> 
> object my_python_exception =
> class_<ConfigError>("ConfigError", init<char*>)
>    ...
>    ;
> 
>> static void translateConfigError(ConfigError const &e) {
>>     PyErr_SetString(my_python_exception, e.what());
>> }
> 
> 
> than above statement will be perfectly valid
> 
> HTH,
> Mike
> 

So am I right in saying I need something like this:

static object my_custom_exc;

static void translateCustomError(CustomError const &e) {
     PyErr_SetString(my_custom_exc, e.what());
}

void exportCustom()
{
	my_custom_exc = class_<CustomError, bases<std::runtime_error>
			, >("CustomError")
		.def("what", &CustomError::what)
	;
         register_exception_translator<CustomError>(&translateCustomError);
}

Basically, the object has to be a global since the translate function 
needs access to it.

John
=:->
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 253 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20041106/0248768f/attachment.pgp>


More information about the Cplusplus-sig mailing list