471,338 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

try/except/else/finally problem

I'm using:

Python 2.3.2 (#1, Oct 17 2003, 19:06:15) [C] on sunos5
And I'm trying to execute:

#! /usr/bin/env python

try:
f = file('test.txt', 'r')
except IOError:
print 'except'
else:
print 'else'
finally:
print 'finally'
And the results are:

File "./test.py", line 9
finally:
^
SyntaxError: invalid syntax
What am I doing wrong?

Thanks in advance for any help.
Jun 28 '07 #1
5 7075
Ed Jensen wrote:
I'm using:

Python 2.3.2 (#1, Oct 17 2003, 19:06:15) [C] on sunos5
And I'm trying to execute:

#! /usr/bin/env python

try:
f = file('test.txt', 'r')
except IOError:
print 'except'
else:
print 'else'
finally:
print 'finally'
And the results are:

File "./test.py", line 9
finally:
^
SyntaxError: invalid syntax
What am I doing wrong?
You need Python 2.5 for that to work. In older Python versions you have to
nest try...except...else and try...finally.

Peter
Jun 28 '07 #2
[ Ed Jensen <ej*****@visi.com]
try:
f = file('test.txt', 'r')
except IOError:
print 'except'
else:
print 'else'
finally:
print 'finally'
And the results are:

File "./test.py", line 9
finally:
^
SyntaxError: invalid syntax
A finally block isn't allowed to appear together with an except block for
releases previous to 2.5. You need to split your exception handling into
two separate blocks.

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4 (GNU/Linux)

iD8DBQBGg/iun3IEGILecb4RAutfAJwPOVb9HcdtQh9Lr8oCp10PnAqjLQCf TILh
TbU0DcUKHMl0VVHf1g8WGsQ=
=cVzM
-----END PGP SIGNATURE-----

Jun 28 '07 #3
Peter Otten <__*******@web.dewrote:
>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.
Jun 28 '07 #4
Ed Jensen wrote:
Peter Otten <__*******@web.dewrote:
>>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

Jun 28 '07 #5
Ed Jensen <ej*****@visi.comwrites:
I'm using:
Python 2.3.2 (#1, Oct 17 2003, 19:06:15) [C] on sunos5

And I'm trying to execute:

#! /usr/bin/env python

try:
f = file('test.txt', 'r')
except IOError:
print 'except'
else:
print 'else'
finally:
print 'finally'
What am I doing wrong?
You're using syntax that is valid under Python 2.5, but invalid in
earlier Python versions.

<URL:http://www.python.org/dev/peps/pep-0341/>
<URL:http://docs.python.org/whatsnew/pep-341.html>

--
\ "Military justice is to justice what military music is to |
`\ music." -- Groucho Marx |
_o__) |
Ben Finney
Jun 28 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.