idiomatic way to collect and report multiple exceptions?

Aahz aahz at pythoncraft.com
Fri May 7 17:28:05 EDT 2010


In article <mailman.2715.1273204222.23598.python-list at python.org>,
Ben Cohen  <ncohen at ucsd.edu> wrote:
>
>eg -- I'd like to do something like this:
>
>errors = []
>for item in data:
>    try:
>        process(item)
>    except ValidationError as e:
>        errors.append(e)
>raise MultipleValidationErrors(*errors)

First of all, please DO NOT post code with TABs in it.  In fact, you
shouldn't even be writing code with TABs -- TAB characters are obsolete
for new Python code.

I would write your code this way:

errors = []
for item in data:
    try:
        process(item)
    except ValidationError as e:
        errors.append(str(e))
if errors:
    raise MultipleValidationErrors(errors)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

f u cn rd ths, u cn gt a gd jb n nx prgrmmng.



More information about the Python-list mailing list