[Python-Dev] Fwd: try...else

Tim Peters tim.one@home.com
Mon, 1 Jan 2001 15:27:37 -0500


[Guido]
> Thomas just checked this in, using Tim's words:

[   The optional \keyword{else} clause is executed when no
    exception occurs in the \keyword{try} clause.  Exceptions in
    the \keyword{else} clause are not handled by the preceding
    \keyword{except} clauses.

vs
    The optional \keyword{else} clause is executed when the
    \keyword{try} clause terminates by any means other than an
    exception or executing a \keyword{return}, \keyword{continue}
    or \keyword{break} statement.  Exceptions in the \keyword{else}
    clause are not handled by the preceding \keyword{except} clauses.
]

> How is this different from "when control flow reaches the end of the
> try clause", which is what I really had in mind?

Only in that it doesn't appeal to a new undefined phrase, and is (I think)
unambiguous in the eyes of a non-specialist reader (like Robin's friend).
Note that "reaching the end of the try clause" is at best ambiguous, because
you *really* have in mind "falling off the end" of the try clause.  It
wouldn't be unreasonable to say that in:

    try:
         x = 1
         y = 2
         return 1

"x=1" is the beginning of the try clause and "return 1" is the end.  So if
the reader doesn't already know what you mean, saying "the end" doesn't nail
it (or, if like me, the reader does already know what you mean, it doesn't
matter one whit what it says <wink>).

> Using the current wording, this paragraph would have to be
> changed each time a new control-flow keyword is added.  Based
> upon the historical record that's not a grave concern ;-),

It was sure no concern of mine ...

> but I think the new wording relies too much on accidentals such
> as the fact that these are the only control flow altering events.
>
> It may be that control flow is not rigidly defined -- but as it is
> what was really intended, maybe the fix should be to explain the
> right concept rather than the current ad-hoc solution.
> ...

OK, except I don't know how to do that succinctly.  For example, if Java had
an "else" clause, the Java spec would say:

    If present, the "else block" is executed if and only if execution
    of the "try block" completes normally, and then there is a choice:

        If the "else block" completes normally, then the
        "try" statement completes normally.

        If the "else block" completes abruptly for reason S,
        then the "try" statement completes abruptly for reason S.

That is, they deal with control-flow issues via appeal to "complete
normally" and "complete abruptly" (which latter comes in several flavors
("reasons"), such as returns and exceptions), and there are pages and pages
and pages of stuff throughout the spec inductively defining when these
conditions obtain.  It's clear, precise and readable; but it's also wordy,
and we don't have anything similar to build on.

As a compromise, given that we're not going to take the time to be precise
(well, I'm sure not ...):

    The optional \keyword{else} clause is executed if and
    when control flows off the end of the \keyword{try}
    clause.\foonote{In Python 2.0, control "flows off the
    end" except in case of exception, or executing a
    \keyword{return}, \keyword{continue} or \keyword{break}
    statement.}
    Exceptions in the \keyword{else} clause are not handled by
    the preceding \keyword{except} clauses.

Now it's all of imprecise, almost precise, specific to Python 2.0, and
robust against any future changes <wink>.