[Python-Dev] Fwd: try...else
M.-A. Lemburg
mal@lemburg.com
Thu, 28 Dec 2000 15:45:49 +0100
Mark Hammond wrote:
>
> Spotted on c.l.python. Although Pythonwin is mentioned, python.exe
> gives the same results - as does 1.5.2.
>
> Seems a reasonable question...
>
> [Also, if Robin hasn't been invited to join us here, I think it could
> make some sense...]
>
> Mark.
> -------- Original Message --------
> Subject: try...else
> Date: Fri, 22 Dec 2000 18:02:27 +0000
> From: Robin Becker <robin@jessikat.fsnet.co.uk>
> Newsgroups: comp.lang.python
>
> I had expected that in try: except: else
> the else clause always got executed, but it seems not for return
I think Robin mixed up try...finally with try...except...else.
The finally clause is executed even in case an exception occurred.
He does have a point however that 'return' will bypass
try...else and try...finally clauses. I don't think we can change
that behaviour, though, as it would break code.
> PythonWin 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on
> win32.Portions Copyright 1994-2000 Mark Hammond (MarkH@ActiveState.com)
> - see 'Help/About PythonWin' for further copyright information.
> >>> def bang():
> .... try:
> .... return 'return value'
> .... except:
> .... print 'bang failed'
> .... else:
> .... print 'bang succeeded'
> ....
> >>> bang()
> 'return value'
> >>>
>
> is this a 'feature' or bug. The 2.0 docs seem not to mention
> return/continue except for try finally.
> --
> Robin Becker
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev@python.org
> http://www.python.org/mailman/listinfo/python-dev
--
Marc-Andre Lemburg
______________________________________________________________________
Company: http://www.egenix.com/
Consulting: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/