[C++-sig] Exception Translation

David Abrahams dave at boost-consulting.com
Mon Jun 12 16:08:30 CEST 2006


"jesus garcia" <jesus24b at gmail.com> writes:

> I like export the follow simple program
>
> #define REPORT_ERROR(nerr,ErrorMsg) throw error(nerr,ErrorMsg)
>
> where error is a class
>
> Class FileName : public string {
> ...
> Void compose (const string &str, int no, const string &ext)
>    *this=(FileName) str;
>    if (no!=-1) {
>       char aux_str[5];
>       sprintf(aux_str,"%05d",no);
>       *this += aux_str;
>    }
>
>    if (ext!="") *this += "."+ext;
>    else REPORT_ERROR(1005,"compose: need a extension");
> }
> ...

Driving from string is a bad idea for many reasons, but anyway.

> I no have any idea for export de class string in C++ to the class
> string in Python 

You can't; it's already done automatically, but you can't derive from
it, because std::string is mapped into python's "str" type, which
isn't a Boost.Python wrapper class.

If you derive your C++ exception types from std::exception you'll get
automatic translation of the what() string into Python.

> and the error I see the follow page
> http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exception.html
> that shows how catch the error but I do it and the problem persist,

You haven't told us what the problem is.

> someone can help,
>
> I put the solution in a file who contains the BOOST_PYTHON_MODULE
> macro like this follow lines"
>
> #include <boost/python.hpp>
> using namespace boost::python;
>
> struct PodBayDoorException;
>
> void brack(PodBayDoorException const& x)
> {PyErr_SetString(PyExc_UserWarning, x);}
>
> extern void port2python_func();
>
> BOOST_PYTHON_MODULE(_nulo) {
> register_exception_translator<PodBayDoorException>(brack);
> port2python_func();
> ...
> }

I'm not sure what you're trying to do here.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list