[Python-ideas] chained try (was Re: "maybe import"?)

Bruce Leban bruce at leapyear.org
Sun Dec 29 05:39:50 CET 2013


On Sat, Dec 28, 2013 at 12:05 PM, Andrew Barnert <abarnert at yahoo.com> wrote:

> Would there be a use for a sequence like this without except on the end
> (so if they all fail, it just raises)?
>

I think what you're talking about is just leaving the try off the last
clause (as in the example below).


> Can you put an exception type or tuple between the "except try"? What
> about an as clause (so the block could use it)?
>

I started writing a mail with exactly this subject line and discarded it
because of the latter two issues. In code I've recently written I have a
chain of three and while the indentation is ugly, it's not so ugly that it
cries out as a must fix language feature. And the problem is that I really
want to limit to specific exceptions. In one case I have something like
this:

try:
    ....
except KeyError:
    try:
        ....
    except AttributeError:

        try:
            ....

        except IndexError:
            ....


and if I can't specify the specific exception, this is not helpful. On the
other hand

try:
    ....
except KeyError try:
    ....
except AttributeError try:
    ....

except IndexError:
    ....


only really saves me two colons and some whitespace. If the language had
this feature, I'd use it, but that doesn't make it worth adding.

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131228/b6f3c9b1/attachment.html>


More information about the Python-ideas mailing list