By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,042 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,042 IT Pros & Developers. It's quick & easy.

[python] using try: finally: except

P: n/a
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 to catch the exception and still have a
finally block?
This is a fictional example of what I want....

try:
x = 'hello'
except Exception, e:
print "oops"
finally:
y = 'world'
print x," ", y

So I surmise one way to guarantee this does what I need would be to do this:

try:
x = 'hello'
except Exception, e:
print "oops"

y = 'world'
print x," ", y

Wouldn't that guarantee y and the print gets executed no matter what? My
exception catches miscellaneous errors and then processing continues.... I
suspect that wouldn't hold for a complex case where the action in the try
block causes a fatal error of some sort....

David
-------
Tracfone: http://cellphone.duneram.com/index.html
Cam: http://www.duneram.com/cam/index.html
Tax: http://www.duneram.com/index.html

__________________________________________________ _______________
Is your PC infected? Get a FREE online computer virus scan from McAfee®
Security. http://clinic.mcafee.com/clinic/ibuy...n.asp?cid=3963
Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"David Stockwell" <wi*******@hotmail.com> wrote in message
news:ma*************************************@pytho n.org...
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 to catch the exception and still have a finally block?
This is a fictional example of what I want....

try:
x = 'hello'
except Exception, e:
print "oops"
finally:
y = 'world'
print x," ", y


try:
try:
x = 'hello'
except Exception, e:
print "oops"
finally:
y = 'world'
print x," ", y

Of course, in your example it doesn't matter because the 'except' clause
doesn't stop execution from passing to the next line, but if you were doing
something more fatal in the 'except' clause then (I think) you would still
execute the 'finally' block.
--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #2

P: n/a
David Stockwell wrote:
So I surmise one way to guarantee this does what I need would be to do this:

try:
x = 'hello'
except Exception, e:
print "oops"

y = 'world'
print x," ", y

Wouldn't that guarantee y and the print gets executed no matter what? My
exception catches miscellaneous errors and then processing continues.... I
suspect that wouldn't hold for a complex case where the action in the try
block causes a fatal error of some sort....


Not really. It's not very likely (but not impossible) for the except
block you have here to throw an exception. But in more realistic
situations, an exception might happen in the except block, and then
your y='world' statement will not be executed.

To show you that even print "oops" can throw an exception, try
executing the following code:

try:
import sys
sys.stdout = None
x = 'hello'
1/0
except:
print "oops"
y = 'world'
print x," ",y

It should print a traceback, but not "hello world".

The right way is:

try:
try:
x = 'hello'
except:
print "oops"
finally:
y = 'world'
print x," ",y
--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
Jul 18 '05 #3

P: n/a
Carl Banks wrote:
The right way is:

try:
try:
x = 'hello'
except:
print "oops"
finally:
y = 'world'
print x," ",y


I seem to recall reading somewhere that this was a cop-out for some
implementation reason. Is there any word on when or if it's going to be
remedied? It seems unbearably ugly and unintuitive; one of the most
irritating Python warts.

--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
Jul 18 '05 #4

P: n/a
OKB (not okblacke) wrote:
Carl Banks wrote:
The right way is:

try:
try:
x = 'hello'
except:
print "oops"
finally:
y = 'world'
print x," ",y


I seem to recall reading somewhere that this was a cop-out for some
implementation reason. Is there any word on when or if it's going to be
remedied? It seems unbearably ugly and unintuitive; one of the most
irritating Python warts.


I recall differently. I recall reading several times that since
it is completely ambiguous what the programmer meant if both are
specified together, Guido deliberately kept them separate so that
one had to be very explicit about whether the finally was inside
or outside the except. The behaviour of the code is quite different
depending on the order...

-Peter
Jul 18 '05 #5

P: n/a
Peter Hansen wrote:
OKB (not okblacke) wrote:
Carl Banks wrote:
The right way is:

try:
try:
x = 'hello'
except:
print "oops"
finally:
y = 'world'
print x," ",y


I seem to recall reading somewhere that this was a cop-out for some
implementation reason. Is there any word on when or if it's going to be
remedied? It seems unbearably ugly and unintuitive; one of the most
irritating Python warts.


I recall differently. I recall reading several times that since
it is completely ambiguous what the programmer meant if both are
specified together, Guido deliberately kept them separate so that
one had to be very explicit about whether the finally was inside
or outside the except. The behaviour of the code is quite different
depending on the order...

try...except and try...finally are really two completely different
statements with different purposes. It's very unfortunate that try is
used for both of them.

Frankly, when you do a try...finally, you're not really trying. In
the words of the Jedi Master: "Try not. Do, or do not. There is no
try." Which is why I think it should rather be do...finally.
--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
Jul 18 '05 #6

P: n/a
Carl Banks wrote:
Frankly, when you do a try...finally, you're not really trying. In
the words of the Jedi Master: "Try not. Do, or do not. There is no
try." Which is why I think it should rather be do...finally.


I have to disagree (because it's wrong :-) :

try:
print "will this code work?"
x = y
print "no, it won't!"
finally:
print "so it really _was_ 'try'..."

If you used 'do' you might get the impression that the contents
of the 'do' were guaranteed to execute or something...

-Peter
Jul 18 '05 #7

P: n/a
[Peter Hansen, on mixing 'except' with 'finally' clauses]
I recall differently. I recall reading several times that since
it is completely ambiguous what the programmer meant if both are
specified together, Guido deliberately kept them separate so that
one had to be very explicit about whether the finally was inside
or outside the except.


It's more that Guido deliberately separated them. Before Python
0.9.6, you could attach both 'except' and 'finally' clauses to the
same 'try' structure (see Misc/HISTORY in a Python source
distribution). I don't remember the semantics, and that was indeed
the problem: nobody could remember, and half the time guessed wrong.

Jul 18 '05 #8

P: n/a
Tim Peters wrote:
[Peter Hansen, on mixing 'except' with 'finally' clauses]
I recall differently. I recall reading several times that since
it is completely ambiguous what the programmer meant if both are
specified together, Guido deliberately kept them separate so that
one had to be very explicit about whether the finally was inside
or outside the except.

It's more that Guido deliberately separated them. Before Python
0.9.6, you could attach both 'except' and 'finally' clauses to the
same 'try' structure (see Misc/HISTORY in a Python source
distribution). I don't remember the semantics, and that was indeed
the problem: nobody could remember, and half the time guessed wrong.


I'm curious: was it that the order of execution was fixed, regardless
of the order of the 'finally' and 'except' in the source, or was
it still confusing even though the order of execution changed
logically with the order of the statements in the source?
Jul 18 '05 #9

P: n/a
[Tim Peters]
It's more that Guido deliberately separated them. Before Python
0.9.6, you could attach both 'except' and 'finally' clauses to the
same 'try' structure (see Misc/HISTORY in a Python source
distribution). I don't remember the semantics, and that was indeed
the problem: nobody could remember, and half the time guessed wrong.

[Peter Hansen] I'm curious: was it that the order of execution was fixed, regardless
of the order of the 'finally' and 'except' in the source, or was
it still confusing even though the order of execution changed
logically with the order of the statements in the source?


If present, a 'finally' clause had to be the last clause in a
try/except/finally structure. That was enforced by the syntax. The
most common confusion was over whether the code in the 'finally'
clause would execute if an exception was raised during execution of an
'except' clause. That code isn't in the 'try' block, so why should
'finally' apply to it? For that matter, why shouldn't it? That's why
nobody could remember (and I in fact don't remember what happened
then).

Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.