[C++-sig] Python exceptions

Garrick Meeker gmeeker at ilm.com
Sat Jan 8 01:12:34 CET 2005

I'm porting some old code to boost.  I have a function that returns a 
PyObject* and if an error occurs it calls 
PyErr_SetString(PyExc_AttributeError, "...") and returns NULL.  No C++ 
exceptions are involved.  With boost, however, the NULL is converted to 

How can I generate an AttributeError exception?  All I see from the docs 
is that I could define a new class, call 
register_exception_translator(), and throw this class.  Since I'm not 
dealing with existing exception classes, is there a simpler, built-in 
way?  Something like:

throw boost::python::exception(PyExc_AttributeError, "error string");

More information about the Cplusplus-sig mailing list