473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 7310
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: KefX | last post by:
This may have been discussed before, but I'm kind of confused as to why Python doesn't support having both an except ~and~ a finally clause, like this: try: raise RuntimeException except:...
7
by: Robert Brewer | last post by:
Alex Martelli wrote in another thread: > One sign that somebody has moved from "Python newbie" to "good Python > programmer" is exactly the moment they realize why it's wrong to code: > > ...
9
by: David Stockwell | last post by:
In referring to my copy of the python bible, it tells me I can't use all three items 'try' except and finally. I can use the t/f or t/e combinations though What combination can i use if i want...
11
by: redawgts | last post by:
I keep getting this error "local variable 'f' referenced before assignment" in the finally block when I run the following code. try: f = file(self.filename, 'rb') f.seek(DATA_OFFSET)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.