try/except/else/finally problem

Peter Otten __peter__ at web.de
Thu Jun 28 16:08:01 EDT 2007


Ed Jensen wrote:

> Peter Otten <__peter__ at web.de> wrote:
> 
>>> try:
>>>     f = file('test.txt', 'r')
>>> except IOError:
>>>     print 'except'
>>> else:
>>>     print 'else'
>>> finally:
>>>     print 'finally'
> 
>> You need Python 2.5 for that to work. In older Python versions you have
>> to nest try...except...else and try...finally.
> 
> Thanks Peter.
> 
> Given the code above, can you show me what that would look like?  The
> nested version, that is.

try:
    try:
        f = file("test.txt", "r")
    except IOError:
        print "except"
    else:
        print "else"
finally:
    print "finally"

Peter




More information about the Python-list mailing list