Exceptions with additional instance variables

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? Or should I use another approach? And how to set values of these variables at the moment of exception? Should I use PyErr_SetString or PyErr_SetObject?
I'm lost.
Thanks in advance. Kind regards.

On Sun, Dec 21, 2008 at 11:02 PM, <chojrak11@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@python.org http://mail.python.org/mailman/listinfo/capi-sig
-- -- Guilherme H. Polo Goncalves
participants (2)
-
chojrak11@gmail.com
-
Guilherme Polo