try..yield..finally problem: a proposed solution.

Alan Kennedy alanmk at
Thu Jun 5 12:47:41 CEST 2003

Dear All,

After thinking about this some more, I've come to the conclusion that explicitly
raising "Finality" is the best solution to the try..yield..finally problem, like


class Finality(Exception): pass

    yield somefunc()
    raise Finality
except Finality:
    print "And finally"
    print "An exception occurred"


This solution has the following benefits

1. The "finally" keyword is not used, therefore no promises or contracts are

2. The user is *explicitly* made responsible for ensuring that their generator
is resumed as far as the "raise Finality" statement. We all know that explicit
is better than implicit. If the user does not resume into the generator and thus
fails to explicitly "raise Finality", then their "finally" clauses simply don't
get executed.

3. It achieves the intended purpose clearly and concisely, and doesn't involve
any changes to the language syntax or semantics.

Any thoughts?

alan kennedy
check http headers here:
email alan:    

More information about the Python-list mailing list