[C++-sig] Re: Exception translation to python classes

Mike Rovner mike at nospam.com
Mon Sep 22 21:50:11 CEST 2003

Niall Douglas wrote:

> I was wondering if anyone had any experience or could point me to
> some example code for translating C++ exceptions by using
> PyErr_SetObject to the Boost.Python wrap of the appropriate C++
> exception object.

I'm not quite sure what are you talking about.
PyErr_SetString is a simple wrapper for the PyErr_SetObject to make your
life easier

  try: some()
  except Exception, e: recover(e)

You can have any 'e' as you wish. First call set it to a string, second to
any python object.

So in my code:

  static void exc_translator(Env::EGeneric* const& x)
  { PyErr_SetString(PyExc_RuntimeError, x->what() ); }
  ...// later

I use a string but (I suppose) you can put:

  { PyErr_SetObject(PyExc_RuntimeError, python::object(x) ); }

for any wrapped C++ instance 'x'; get it in your python code above as 'e'
and go from there, say call any (wrapped) method of x.


More information about the Cplusplus-sig mailing list