<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">I've condensed the advice from this thread into this.<div><br><div><div>import sys</div><div>import traceback</div><div><br></div><div>class ExceptionList(object):</div><div>   def __init__(self, msg, errors=None, *args):</div><div>       self.errortb = errors or []</div><div>       super(ExceptionList, self).__init__(msg, *args)</div><div><br></div><div>   def __str__(self):</div><div>       """Print a pretty traceback of captured tracebacks</div><div>       """</div><div><br></div><div>       tracebacks = '\n'.join(self.errortb)</div><div>       msg = super(ExceptionList, self).__str__()</div><div>       parts=(msg, "="*78, tracebacks)</div><div>       msg = '\n'.join(parts)</div><div>       return msg</div><div><br></div><div>def capture_traceback(limit=None):</div><div>    tb = traceback.format_exception(limit=None, *sys.exc_info())</div><div>    tb = "".join(traceback for traceback in tb)</div><div>    return tb</div><div><br></div><div>class SomeException(ExceptionList, Exception):</div><div>    pass</div><div><br></div><div>errors = []</div><div><br></div><div>for c in 'hello':</div><div>    try:</div><div>        int(c)</div><div>    except ValueError:</div><div>        errors.append(capture_traceback())</div><div>if errors:</div><div>    raise SomeException('Multiple Exceptions encountered, see below tracebacks', errors)</div><div><br></div></div><div>This is like Aahz's method but I wanted to include the whole traceback and his example only captures the exception's error message.  I changed the example from Chris to only capture the text of the traceback rather than the whole traceback.</div><div><div><br></div><div>As for whether this is terribly strange or not useful --</div><div><br></div><div>Steven said</div><div><blockquote type="cite">That's a nice trick, but I'd really hate to see it in real life code. <br>Especially when each traceback was deep rather than shallow. Imagine <br>having fifty errors, each one of which was ten or twenty levels deep!<font class="Apple-style-span" color="#000000"><font class="Apple-style-span" color="#144FAE"><br></font></font></blockquote><div><br></div>I don't want to get buried in overlong stack traces either so I included the optional limit parameter to the format_traceback call --<br><br><blockquote type="cite">I also question how useful this would be in real life. Errors can cascade <br>in practice, and so you would very likely get spurious errors that were <br>caused by the first error. You see the same thing in doctests, e.g. if <br>you do this:<br></blockquote></div><div><br></div><div>I think this has use as in the example case -- when sequentially processing the elements of a list with the processing of each element being sufficiently 'independent' of the processing of the other elements so that the errors won't cascade.  This seems handy for debugging situations like that, as might be common when developing a command line tool...  Or as in my case, when 'validating' a sequence of elements.</div><div><br></div><div>I'd be curious if others think this use seems odd or a bad idea ...</div><div><br></div><div>Thanks to all for the free advice!</div><div>Ben</div><div><br></div><div><div><div><div><div><div>On May 7, 2010, at 7:24 PM, Chris Rebert wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div><blockquote type="cite">On May 6, 2010, at 10:56 PM, Chris Rebert wrote:<br></blockquote><blockquote type="cite"><blockquote type="cite">On Thu, May 6, 2010 at 8:50 PM, Ben Cohen <<a href="mailto:ncohen@ucsd.edu">ncohen@ucsd.edu</a>> wrote:<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">Is there a pythonic way to collect and display multiple exceptions at the same time?<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">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.<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">eg -- I'd like to do something like this:<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">errors = []<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">for item in data:<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">       try:<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">               process(item)<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">       except ValidationError as e:<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">               errors.append(e)<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">raise MultipleValidationErrors(*errors)<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">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 ...<br></blockquote></blockquote></blockquote><my implementation snipped><br><br>On Fri, May 7, 2010 at 6:06 PM, Ben Cohen <<a href="mailto:ncohen@ucsd.edu">ncohen@ucsd.edu</a>> wrote:<br><blockquote type="cite">Many thanks for the excellent example!!  You rock!<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Ben<br></blockquote><br>However, I do agree with Steven that this approach to error handling<br>is unusual. But I assume you have your reasons for wanting to do it<br>this way.<br><br>Cheers,<br>Chris<br>--<br>Python + Bioinformatics = Win<br><a href="http://blog.rebertia.com">http://blog.rebertia.com</a><br></div></blockquote></div><br></div></div></div></div></div></div></body></html>