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

Samuel Littley samuel.littley at toastwaffle.com
Mon Jun 24 21:13:12 CEST 2013


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



More information about the Python-ideas mailing list