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

Andrew Barnert abarnert at yahoo.com
Tue Jun 25 00:42:40 CEST 2013


On Jun 24, 2013, at 13:13, Anders Hovmöller <boxed at killingar.net> wrote:

> Why not just put those exceptions in a list instead of raising them? So instead of
> 
> raise Exception(...)
> 
> errors.append(Exception(....))

And if you're trying to call code that might raise, just wrap each call/iteration/whatever. Something like this:

errors = []
for test in tests:
    try:
       do_test(test)
    except Exception as e:
        errors.append(e)

You can also easily wrap the try-and-append up as a function (either a closure, or a method on an object that holds a self.errors), or even a decorator.

There doesn't seem to be any need for new syntax here.

> 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
> 
> _______________________________________________
> 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/710346f3/attachment.html>


More information about the Python-ideas mailing list