[Python-Dev] args attribute of Exception objects

Raymond Hettinger python at rcn.com
Tue Apr 12 14:03:50 CEST 2005


[Sébastien de Menten]
>  2) Could this be changed to .args more in line with:
>     a) first example: e.args = ('foo', "NameError: name 'foo' is not
> defined")
>     b) second example: e.args = (4, 'foo', "'int' object has no
attribute
> 'foo'",)
>   the message of the string can even be retrieved with str(e) so it is
> also
> redundant.

Something like this ought to be explored at some point.  It would
certainly improve the exception API to be able to get references to the
objects without parsing strings.

The balancing forces are backwards compatibility and a need to keep the
exception mechanism as lightweight as possible.

Please log a feature request on SF.  Note that the idea is only for
making builtin exceptions more informative.  User defined exceptions can
already attach arbitrary objects:

>>> class Boom(Exception):
        pass

>>> x = 10
>>> if x != 5:
        raise Boom("Value must be a five", x)

Traceback (most recent call last):
  File "<pyshell#12>", line 2, in -toplevel-
    raise Boom("Value must be a five", x)
Boom: ('Value must be a five', 10)


Raymond Hettinger


More information about the Python-Dev mailing list