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

Alan Kennedy alanmk at hotmail.com
Thu Jun 5 06:47:41 EDT 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
so:

#--------------------------------

class Finality(Exception): pass

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

#--------------------------------

This solution has the following benefits

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

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: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list