What's the use of the else in try/except/else?

Peter Pearson ppearson at nowhere.invalid
Tue May 12 11:45:09 EDT 2009


On 12 May 2009 09:35:36 GMT, Steven D'Aprano wrote:
[snip]
> To really be safe, that should become:
>
> try:
>     rsrc = get(resource)
> except ResourceError:
>     log('no more resources available')
>     raise
> else:
>     try:
>         do_something_with(rsrc)
>     finally:
>         rsrc.close()

Thanks, Steven.  I find these examples illuminating.

Not trying to be dense, but given that the "except" block
re-raises the exception, isn't the above the same as . . .?

try:
    rsrc = get(resource)
except ResourceError:
    log('no more resources available')
    raise
try:
    do_something_with(rsrc)
finally:
    rsrc.close()

-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list