Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What about try:except:finally

0 views
Skip to first unread message

Thomas Weholt

unread,
Jul 7, 2000, 3:00:00 AM7/7/00
to
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

Olivier Dagenais

unread,
Jul 7, 2000, 3:00:00 AM7/7/00
to
If I remember the tutorial correctly, it doesn't support finally with
anything else. I have a feeling you would need to do:

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

Peter Schneider-Kamp

unread,
Jul 7, 2000, 3:00:00 AM7/7/00
to Thomas Weholt
Thomas Weholt wrote:
>
> try:
> # some code
> except :
> # catch exception
> finally:
> # clean up whatever

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


Richard Gruet

unread,
Jul 7, 2000, 3:00:00 AM7/7/00
to

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


Jon McLin

unread,
Jul 7, 2000, 3:00:00 AM7/7/00
to
try:
try:
# some code
except:
# catch exception
finally:
#clean up

Alan Daniels

unread,
Jul 8, 2000, 3:00:00 AM7/8/00
to
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:

try:
x = 1
except NameError:
print "Ack!"
else:
print "Woohoo!"

--
============================================================
Alan Daniels
dan...@mindspring.com

Chris Withers

unread,
Jul 8, 2000, 3:00:00 AM7/8/00
to dan...@mindspring.com
Alan Daniels wrote:
> >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:

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


Thomas Wouters

unread,
Jul 8, 2000, 3:00:00 AM7/8/00
to pytho...@python.org

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!


Matthew Schinckel

unread,
Jul 9, 2000, 3:00:00 AM7/9/00
to
On Jul 8, I overheard Alan Daniels mutter:

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


Martin von Loewis

unread,
Jul 9, 2000, 3:00:00 AM7/9/00
to
Matthew Schinckel <ba...@null.net> writes:

> 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


Tim Peters

unread,
Jul 9, 2000, 3:00:00 AM7/9/00
to pytho...@python.org
[Martin von Loewis]

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


Tomek Lisowski

unread,
Jul 12, 2000, 3:00:00 AM7/12/00
to
Użytkownik Olivier Dagenais <olivierS....@canadaA.comM> w wiadomości
do grup dyskusyjnych
napisał:rkl95.48923$W35.1...@news20.bellglobal.com...

> If I remember the tutorial correctly, it doesn't support finally with
> anything else. I have a feeling you would need to do:
>
> 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.

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


Olivier Dagenais

unread,
Jul 12, 2000, 3:00:00 AM7/12/00
to
Yes, I was going on the assumption that #init wasn't going to throw an
exception, and that the inner "except" block would re-throw if it was really
bad:

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

0 new messages