473,233 Members | 2,113 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,233 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 7297
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)...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.