idiomatic way to collect and report multiple exceptions?

Ben Cohen ncohen at ucsd.edu
Thu May 6 23:50:01 EDT 2010


Is there a pythonic way to collect and display multiple exceptions at the same time?

For example let's say you're trying to validate the elements of a list and you'd like to validate as many of the elements as possible in one run and still report exception's raised while validating a failed element.

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)

where if the raised MultipleValidationErrors exception goes uncaught the interpreter will print a nice traceback that includes the tracebacks of each raised ValidationError.  But I don't know how MultipleValidationErrors should be written ...  I'm targeting python 2.6 at the moment -- googling around for advice I see some pep's relating to exception handling changes in python3 that might be relevant but I've ended up more confused.

Thanks in advance,

Ben


More information about the Python-list mailing list