[Python-ideas] Catching of multiple exceptions without halting execution

Anders Hovmöller boxed at killingar.net
Mon Jun 24 22:13:24 CEST 2013


Why not just put those exceptions in a list instead of raising them? So
instead of

raise Exception(...)

errors.append(Exception(....))


On Mon, Jun 24, 2013 at 9:13 PM, Samuel Littley <
samuel.littley at toastwaffle.com> wrote:

> I find I often have to run several tests on data input, making a list of
> which tests succeed, which fail, and somehow determine where to go after
> running all the tests. Obviously, the standard way of showing that a
> test as failed is to raise an exception, caught by try/except/finally,
> however this would only allow one test to be flagged as failing,
> requiring multiple runs to correct every fault that may exist.
>
> I propose an alternative to try/except, as follows:
>
> validate:
>     // Code to run tests, raising exceptions if tests fail
> accept:
>     // Code to run if all tests pass (i.e. no exceptions)
> reject es:
>     // Code to handle each failed test
> except:
>     // Code to handle non-test related exceptions
> finally:
>     // Code to be always executed
>
> The difference to a normal try/except is a different type of exception,
> which, rather than halting execution, is added to the list `es`, which
> the reject block could then loop through to display error messages,
> require re-entry, etc. Standard exceptions could be raised and caught by
> the except block. Alternatively, the except block could not be a part of
> this, and instead all exceptions are caught in the reject block, which
> could then raise exceptions itself to be caught by a try/except/finally
> around the validate/accept/reject/finally
>
> The use case I thought of is validating data entry (from web forms for
> example), where each exception creates an error message displayed on the
> form, however I'm pretty sure there would be other uses for this.
>
> Samuel Littley
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130624/ad536d35/attachment.html>


More information about the Python-ideas mailing list