473,400 Members | 2,145 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,400 software developers and data experts.

[python] using try: finally: except

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
9 3789
"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
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
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
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
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
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
[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
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
[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
8
by: Ray | last post by:
Hello guys, OK, I've been reading some more about Python. There are some things about Python exception that I haven't been able to grasp: 1. This is a small thing, but why is object spelled...
17
by: nnorwitz | last post by:
For more details about the plan for Python 2.5, see: http://www.python.org/doc/peps/pep-0356/ The highlights are that we are hoping to put out the first alpha Real Soon Now, hopefully in a...
22
by: Kurien Mathew | last post by:
Hello, Any suggestions on a good python equivalent for the following C code: while (loopCondition) { if (condition1) goto next; if (condition2) goto next;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.