Question about exception-handling mechanism

Fredrik Lundh effbot at
Tue Apr 25 10:06:21 EDT 2000

willfg at wrote:
>    I'm new to Python, but find its exception handling mechanism much
> more powerful than comparable languages; but a question was put to me
> that being new I can't come up with a very articulate answer. A
> colleague asked why in an exception handling mechanism you'd want the
> ELSE block to be executed if you don't throw an exception as opposed to
> a FINALLY block. Anyone used this feature in practice? Thanks in
> advance for your input.

the standard pattern for try/except/else is:

        do something
    except SomeError:
        deal with error
        deal with success

this could also be written:

        do something
        deal with success
    except SomeError:
        deal with error

but this only works if the "deal with success"
code cannot raise a SomeError exception...


try-finally is different; the code in the finally
clause will be executed in either case, but if
there was an exception, it's reraised after the
finally clause has finished:

    create a mess
        raise SomeError
        clean up
    this line will never be reached

hope this helps!


<!-- (the eff-bot guide to) the standard python library:

More information about the Python-list mailing list