[capi-sig] Exceptions with additional instance variables

Guilherme Polo ggpolo at gmail.com
Mon Dec 22 03:29:31 CET 2008

On Sun, Dec 21, 2008 at 11:02 PM,  <chojrak11 at gmail.com> wrote:
> Hello,
> I'm trying to implement custom exception that have to carry some
> useful info by means of instance members, to be used like:
> try:
>    // some code
> except MyException, data:
>    // use data.errorcode, data.errorcategory, data.errorlevel,
> data.errormessage and some others
> The question is - how to implement the instance variables with
> PyErr_NewException?

Using PyErr_NewException is fine. You must understand that an
exception is a class, and thus PyErr_NewException creates one for you
and returns it.

> Or should I use another approach? And how to set
> values of these variables at the moment of exception?

Just like you would do with a class that has __dict__, set some
attributes to what you want. That is, use PyObject_SetAttrString or
something more appropriated for you.

> Should I use
> PyErr_SetString or PyErr_SetObject?

In both cases you are just specifying the error message, so you use
what is better for you to provide the error message.

> I'm lost.
> Thanks in advance.
> Kind regards.
> _______________________________________________
> capi-sig mailing list
> capi-sig at python.org
> http://mail.python.org/mailman/listinfo/capi-sig

-- Guilherme H. Polo Goncalves

More information about the capi-sig mailing list