Generating exceptions from C

Just just at
Sat Mar 18 16:06:03 CET 2006

In article <mailman.3346.1142693340.27775.python-list at>,
 Jacob Kroon <jacob.kroon at> wrote:

> I'll just reply to myself what I've found out so far:
> > 1. PyErr_NewException() creates the exception _class_, not the 
> > instance right ?
> >
> Looks like it does yes. It doesn't even seem right to talk about an 
> _instance_ of an exception...
> > 2. Is PyErr_SetString() the correct way to raise exceptions ?
> After looking at error.c in the python sources, it looks like that 
> function sets
> the global exception type and value variables to the provided arguments.
> > 3. Besides the error message I pass to PyErr_SetString(), I also want 
> > to pass additional return
> >   data together with the exception. But this should be attached to the 
> > exception _instance_,
> >   not the class, am I right ?
> >
> The right way seems to be to create a tuple that consists of the string 
> message, and any additional data,
> and pass the tuple in PyErr_SetObject().
> > 4. If I am supposed to attach it to the exception instance, how would 
> > I do that ? I never have a
> >   pointer to the exception instance, just the class.
> As said previously, there is never an instance of the exception, data is 
> passed in the "value" argument.
> Does the comments above make sense?

Not quite: when raising an exception, an instance of the exception class 
_is_ created. Just like this:

   raise SomeException(msg)

With the old (deprecated) spelling the instantiation is done implicitly:

   raise SomeException, msg


More information about the Python-list mailing list