[Python-Dev] Chaining try statements: eltry? (original) (raw)

Thomas Lotze thomas at thomas-lotze.de
Wed Jul 6 20:07:45 CEST 2005


Hi,

this is my first message to this list, so let me say hello first.

I want to ask what you think about introducing a keyword 'eltry' which would be the counterpart of 'elif' for try statements. This had been suggested before on python-list a couple of years ago by Jonathan Gardner, but nothing (that I could find) seems to have come of it.

Starting from PEP 341, the try statement might be extended to

try_stmt: 'try' ':' suite ( (except_clause ':' suite)+ ( 'eltry' ':' suite (except_clause ':' suite)+ )* ['else' ':' suite] ['finally' ':' suite] | 'finally' ':' suite )

where lines 4 through 7 would be an addition to the definition given in the PEP. Admittedly, this looks somewhat complex, but I don't think it actually is. (And while an new keyword always means potentially breaking code, 'eltry' doesn't seem to me a likely identifier chosen by programmers - YMMV.)

This extension would be useful whenever a suite of statements is to be terminated as soon as one of them raises an exception, and the programmer is interested in handling those individually. Without 'eltry' the choices are:

An example would be a filter chain:

def use_eltry(data): # decode as far as possible try: data = foo_decode(data) except ValueError: print "Data seems not to be foo encoded." eltry: data = bar_decode(data) except ValueError: print "Foo decoded data seems not to be bar encoded." eltry: data = baz_decode(data) except ValueError: print "FooBar decoded data seems not to be baz encoded."

# whatever data is by now, do something with it
do_something(data)

def use_stacking(data): # decode as far as possible try: data = foo_decode(data) except ValueError: print "Data seems not to be foo encoded." else: try: data = bar_decode(data) except ValueError: print "Foo decoded data seems not to be bar encoded." else: try: data = baz_decode(data) except ValueError: print "FooBar decoded data seems not to be baz encoded."

# whatever data is by now, do something with it
do_something(data)

def use_single_handler(data): # decode as far as possible try: data = foo_decode(data) data = bar_decode(data) data = baz_decode(data) except ValueError: print "Data could not be decoded, can't tell you why easily."

# whatever data is by now, do something with it
do_something(data)

def use_custom_exception(data): # decode as far as possible try: try: data = foo_decode(data) except ValueError: print "Data seems not to be foo encoded." raise

    try:
        data = bar_decode(data)
    except ValueError:
        print "Foo decoded data seems not to be bar encoded."
        raise

    try:
        data = baz_decode(data)
    except ValueError:
        print "FooBar decoded data seems not to be baz encoded."
        raise
except ValueError:
    pass

# whatever data is by now, do something with it
do_something(data)

def use_function(data): # decode as far as possible def helper(): try: data = foo_decode(data) except ValueError: print "Data seems not to be foo encoded." return

    try:
        data = bar_decode(data)
    except ValueError:
        print "Foo decoded data seems not to be bar encoded."
        return

    try:
        data = baz_decode(data)
    except ValueError:
        print "FooBar decoded data seems not to be baz encoded."
        return

helper()

# whatever data is by now, do something with it
do_something(data)

Does this seem contrived? (Obviously, I don't think so.) It would be nice to hear your opinions and, if you don't agree with the proposal, learn what is bad about it.

--

Viele Grüße, Thomas



More information about the Python-Dev mailing list