2005-05-07 08:13:49 -04:00
|
|
|
PEP: 341
|
|
|
|
Title: Unifying try-except and try-finally
|
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
2007-05-01 16:39:17 -04:00
|
|
|
Author: Georg Brandl <georg@python.org>
|
2006-02-08 00:53:10 -05:00
|
|
|
Status: Final
|
2005-05-07 08:13:49 -04:00
|
|
|
Type: Standards Track
|
2017-01-10 01:52:57 -05:00
|
|
|
Content-Type: text/x-rst
|
2005-05-07 08:13:49 -04:00
|
|
|
Created: 04-May-2005
|
2022-08-24 18:40:18 -04:00
|
|
|
Python-Version: 2.5
|
2005-05-07 08:13:49 -04:00
|
|
|
Post-History:
|
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
2017-01-10 01:52:57 -05:00
|
|
|
========
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
This PEP proposes a change in the syntax and semantics of try
|
|
|
|
statements to allow combined try-except-finally blocks. This
|
|
|
|
means in short that it would be valid to write::
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
try:
|
|
|
|
<do something>
|
|
|
|
except Exception:
|
|
|
|
<handle the error>
|
|
|
|
finally:
|
|
|
|
<cleanup>
|
2005-05-07 08:13:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
Rationale/Proposal
|
2017-01-10 01:52:57 -05:00
|
|
|
==================
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
There are many use cases for the try-except statement and
|
|
|
|
for the try-finally statement per se; however, often one needs
|
|
|
|
to catch exceptions and execute some cleanup code afterwards.
|
|
|
|
It is slightly annoying and not very intelligible that
|
|
|
|
one has to write::
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
f = None
|
|
|
|
try:
|
2005-05-07 08:13:49 -04:00
|
|
|
try:
|
2017-01-10 01:52:57 -05:00
|
|
|
f = open(filename)
|
|
|
|
text = f.read()
|
|
|
|
except IOError:
|
|
|
|
print 'An error occurred'
|
|
|
|
finally:
|
|
|
|
if f:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
So it is proposed that a construction like this::
|
|
|
|
|
|
|
|
try:
|
|
|
|
<suite 1>
|
|
|
|
except Ex1:
|
|
|
|
<suite 2>
|
|
|
|
<more except: clauses>
|
|
|
|
else:
|
|
|
|
<suite 3>
|
|
|
|
finally:
|
|
|
|
<suite 4>
|
|
|
|
|
|
|
|
be exactly the same as the legacy::
|
|
|
|
|
|
|
|
try:
|
2005-05-07 08:13:49 -04:00
|
|
|
try:
|
|
|
|
<suite 1>
|
|
|
|
except Ex1:
|
|
|
|
<suite 2>
|
|
|
|
<more except: clauses>
|
|
|
|
else:
|
|
|
|
<suite 3>
|
2017-01-10 01:52:57 -05:00
|
|
|
finally:
|
|
|
|
<suite 4>
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
This is backwards compatible, and every try statement that is
|
|
|
|
legal today would continue to work.
|
2005-05-07 08:13:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
Changes to the grammar
|
2017-01-10 01:52:57 -05:00
|
|
|
======================
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
The grammar for the try statement, which is currently::
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
try_stmt: ('try' ':' suite (except_clause ':' suite)+
|
|
|
|
['else' ':' suite] | 'try' ':' suite 'finally' ':' suite)
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
would have to become::
|
|
|
|
|
|
|
|
try_stmt: 'try' ':' suite
|
|
|
|
(
|
|
|
|
(except_clause ':' suite)+
|
|
|
|
['else' ':' suite]
|
|
|
|
['finally' ':' suite]
|
|
|
|
|
|
|
|
|
'finally' ':' suite
|
|
|
|
)
|
2005-05-07 08:13:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
Implementation
|
2017-01-10 01:52:57 -05:00
|
|
|
==============
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
As the PEP author currently does not have sufficient knowledge
|
|
|
|
of the CPython implementation, he is unfortunately not able
|
|
|
|
to deliver one. Thomas Lee has submitted a patch [2]_.
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
However, according to Guido, it should be a piece of cake to
|
|
|
|
implement [1]_ -- at least for a core hacker.
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
This patch was committed 17 December 2005, SVN revision 41740 [3]_.
|
2006-02-08 00:53:10 -05:00
|
|
|
|
2005-05-07 08:13:49 -04:00
|
|
|
|
|
|
|
References
|
2017-01-10 01:52:57 -05:00
|
|
|
==========
|
2005-05-07 08:13:49 -04:00
|
|
|
|
2017-06-11 15:02:39 -04:00
|
|
|
.. [1] https://mail.python.org/pipermail/python-dev/2005-May/053319.html
|
2018-07-21 19:57:17 -04:00
|
|
|
.. [2] https://bugs.python.org/issue1355913
|
2017-06-11 15:02:39 -04:00
|
|
|
.. [3] https://mail.python.org/pipermail/python-checkins/2005-December/048457.html
|
2005-05-07 08:13:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
2017-01-10 01:52:57 -05:00
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
2005-05-07 08:13:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
sentence-end-double-space: t
|
|
|
|
fill-column: 70
|
|
|
|
End:
|