Try block problem

Skip Montanaro skip at pobox.com
Tue Nov 20 14:41:05 CET 2001


    Marcin> Tue, 20 Nov 2001 05:37:41 -0600, Skip Montanaro <skip at pobox.com> pisze:
    >> The thing that is perhaps slightly misleading is the use of the "try"
    >> keyword to begin both constructs.  There's really no "try"
    >> connotation to try/finally.

    Marcin> Maybe it's because try:except: would have to be used to simulate
    Marcin> try:finally: if it wasn't available natively.

I don't know how you'd do this.  For example:

    def f(d):
        try:
            return d.keys()
        finally:
            d.clear()

How would that be handled with try/except?  (IOW, where's the exception?)
The natural way to code it (without try/finally) would be

    def f(d):
        keys = d.keys()
        d.clear()
        return keys

    Marcin> In other words try:finally: is one of things which do something
    Marcin> non-trivial with exceptions instead of silently propagating them
    Marcin> up.

Try/finally isn't directly concerned with exceptions at all.

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list