[python] using try: finally: except

D

David Stockwell

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/campaign.asp?cid=3963
 
R

Russell Blau

David Stockwell said:
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.
 
C

Carl Banks

David said:
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
 
O

OKB (not okblacke)

Carl said:
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
 
P

Peter Hansen

OKB said:
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
 
C

Carl Banks

Peter said:
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.
 
P

Peter Hansen

Carl said:
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
 
T

Tim Peters

[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.
 
P

Peter Hansen

Tim said:
[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?
 
T

Tim Peters

[Tim Peters]
[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).
 
A

Antoon Pardon

Op 2004-06-25 said:
[Tim Peters]
[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?

Well personnaly I wouldn't find it that hard to aswer this.

The finally clause is to finalise code in the try block.
That an exception occured in an except clause doesn't change
that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top