Preferred exception style?
Bengt Richter
bokr at oz.net
Fri May 30 20:48:40 EDT 2003
On Fri, 30 May 2003 14:00:00 -0600, Steven Taschuk <staschuk at telusplanet.net> wrote:
>Quoth George Young:
> [...]
>> Furthermore, the parenthesis form allows for more than just
>> a string value. E.g.:
>> class GetRunFailed(Exception):
>> def __init__(self, reqobj, dberrormsg=None,
>> domainerr='Run does not exist'):
> [...]
>
>Positional parameters (but not keyword arguments) can be passed
>with the comma syntax, using a tuple in the second place:
>
> raise GetRunFailed, (reqobj, dberrormsg, domainerr)
>
>The familiar special case for 1-tuples occurs here too, of course.
>
ISTM it could be useful to use keyword parameters and just do self.__dict__.update(kw)
so they'd show up as exception instance attributes, like errno, etc., e.g.,
>>> class X2(Exception):
... def __init__(self, *args, **kw):
... Exception.__init__(self, *args)
... self.__dict__.update(kw)
...
>>> try:
... raise X2(1,2,3, one=1,two=2)
... except Exception, e:
... pass
...
>>> e
<__main__.X2 instance at 0x007A9C50>
>>> vars(e)
{'args': (1, 2, 3), 'two': 2, 'one': 1}
>>>
>>> e.__class__.__name__
'X2'
Regards,
Bengt Richter
More information about the Python-list
mailing list