[Python-ideas] Arguments to exceptions
Jeff Walker
jeff.walker00 at yandex.com
Sun Jul 16 19:17:39 EDT 2017
15.07.2017, 18:33, "Chris Angelico" <rosuav at gmail.com>:
> On Sun, Jul 16, 2017 at 10:12 AM, Jeff Walker <jeff.walker00 at yandex.com> wrote:
>> The first problem is that there is no direct access to the components that make up the error in some of the standard Python exceptions.
>>
>> >>> foo
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> NameError: name 'foo' is not defined
>>
>> If you need access to the name, you must de-construct the error message. To get direct access to the name, it would need to be passed to the exception when raised. Why wasn't that done?
>
> Because it normally isn't needed. Can you give an example of where
> NameError could legitimately be raised from multiple causes?
For me it generally occurs when users are using Python to hold their actual data. Python can be used as a data format in much the same way JSON or Yaml. And if the end user is familiar with Python, it provides many nice benefits over the alternatives. I tend to use it in this fashion a great deal, particularly for configuration information. In these case, it would be nice to be able access the 'participants' in the following exceptions:
NameError (offending name)
KeyError (collection, offending key)
IndexError (collection, offending index)
Jeff
More information about the Python-ideas
mailing list