473,785 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 3829
"David Stockwell" <wi*******@hotm ail.com> wrote in message
news:ma******** *************** **************@ python.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
2416
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 narrowed it down to the Scrubber.py file.. If this seems like a quick step me through, I would be very appreciative, could get you something on your Amazon wish-list (that is me on my knees begging).. From just my basic understanding, it...
7
3671
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. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
8
1982
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 "object", and the mother of all exception "Exception" (with capital E)? Why is not object spelled "Object" then? Especially since Exception's descendants are named with the first letter of each word capitalized? I mean, in Java, it's Object....
17
1878
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 week or two. If there's something you really think just must be in 2.5 and can't wait until 2.6, now is the time to speak up. (Actually, it was time to speak up months ago.) Send a message to python-dev. If you have a patch or bug report...
22
7922
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
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.