Does python support, or if not, would it be cool to have support for :
try:
# some code
except :
# catch exception
finally:
# clean up whatever
Thomas
try:
#init
try:
# stuff
except:
# stuff
finally:
#stuff
...that is, assuming that your "except" would only catch specific exceptions
that you would want to prepare an error message with.
--
----------------------------------------------------------------------
Olivier A. Dagenais - Carleton University - Computer Science III
"Thomas Weholt" <tho...@cintra.no> wrote in message
news:3967d908....@news.online.no...
You can always use
try:
try:
# some code
except:
# catch exception
finally:
# clean up whatever
Not quite as elegant, but do we really need
an extra syntax for that?
wanting-orthogonal-persistence-ly y'rs
Peter
--
Peter Schneider-Kamp ++47-7388-7331
Herman Krags veg 51-11 mailto:pe...@schneider-kamp.de
N-7050 Trondheim http://schneider-kamp.de
Thomas Weholt wrote:
> Hi,
>
> Does python support, or if not, would it be cool to have support for :
>
> try:
> # some code
> except :
> # catch exception
> finally:
> # clean up whatever
>
> Thomas
no, these are 2 separate constructs that you can embed:
try:
try:
# some code
except:
# catch exception
finally:
# clean-up whatever
Your proposition makes sense but it's little gain, and with separate
constructs you can do a lot of different things in the SAME try/finally,
e.g:
# Allocate resource (eg. open file)
try:
# do something
try:
# some code
except:
# catch exception
# do something else
try:
# some code
except:
# catch exception
finally:
# release resource (eg close file)
Cheers,
Richard
[paraphrased...]
>Does python support, or if not, would it be cool to have support for
>try/except/finally?
No, but just use try/except/else instead. Why it's "else" instead of
"finally", I have no idea, but this works just fine. Example:
try:
x = 1
except NameError:
print "Ack!"
else:
print "Woohoo!"
--
============================================================
Alan Daniels
dan...@mindspring.com
Actually, yes it does ;-)
...but it does something different. I read the discription on page 50 of
David Beazely's Python Essential Reference, but it didn't make much
sense to me :S
cheers,
Chris
The 'else' is called 'else' because it's similar to the 'else' in 'if'
clauses and 'for' and 'while' loops: the 'else' clause of a 'try/except'
only gets executed when *no* exception occurs. 'finally', however, will
alway be called on the 'way out' of the try-block, wether something went
wrong or not.
--
Thomas Wouters <tho...@xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
> On Fri, 07 Jul 2000 13:20:25 GMT, Thomas Weholt <tho...@cintra.no> wrote:
>
> [paraphrased...]
> >Does python support, or if not, would it be cool to have support for
> >try/except/finally?
>
> No, but just use try/except/else instead. Why it's "else" instead of
> "finally", I have no idea, but this works just fine. Example:
Yes, but if you want a statement executed regardless of whether there was
an exception or not, you need to use a finally clause.
--
Matthew Schinckel <ma...@null.net>
What this country needs is a good five cent microcomputer.
> Yes, but if you want a statement executed regardless of whether there was
> an exception or not, you need to use a finally clause.
That also shows that having try:except:finally is perhaps not a good
idea. Given
try:
A
except:
B
finally:
C
is that the same as
try:
try:
A
finally:
C
except:
B
or as
try:
try:
A
except:
B
finally:
C
or is it something else? If so, what would it be? And why not one of
the others?
Regards,
Martin
Sorry that I haven't followed this whole thread, but if no old-timer has
pointed it out before, Python *used* to allow try/except/finally all at the
same level. For exactly the reason Martin gave here, that was removed
before Python's first public release. From Misc/HISTORY:
New features in 0.9.6:
- stricter try stmt syntax: cannot mix except and finally clauses on 1
try
The ambiguity Martin pointed out here was real: it's not at all obvious
what mixing them means, and about half <wink> the users guessed wrong about
what the implementation actually did.
explicit-is-better-than-implicit-ly y'rs - tim
Please quote the original message next time.
At least I can see in your example one error. If you place try...except...
within try...finally..., the latter construct won't see any exceptions at
all. Moreover, if #init raises an exception, it won't be caught!
I see it more logical in this order:
try:
try:
#init
#stuff
except:
#stuff
finally:
#stuff
If you want separate behaviour for exceptions in #init and the first #stuff
sections, add yet another try...except... contruct. If #init is safe, and
will not raise any exceptions, it can be placed outside of the inner
try...except... construct.
Tomek Lisowski
try:
# safe init stuff
try:
# not so safe stuff
except:
# is it an exception we want to handle?
# if so, handle, send an error message, etc..
# otherwise, re-throw the error
finally:
# de-init stuff
--
----------------------------------------------------------------------
Olivier A. Dagenais - Carleton University - Computer Science III
"Tomek Lisowski" <Lisowsk...@sssa.nospam.pl> wrote in message
news:ICWa5.46250$Qw1.1...@news.tpnet.pl...
> U?ytkownik Olivier Dagenais w wiadomo?ci
> do grup dyskusyjnych
> napisa?:rkl95.48923$W35.1...@news20.bellglobal.com...