Visitar URL original
gh-95975: Move except/*/finally ref labels to more precise locations by CAM-Gerlach · Pull Request #95976 · python/cpython · GitHub
Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,8 @@ returns the list ``[0, 1, 2]``.
.. versionchanged:: 3.11
Starred elements are now allowed in the expression list.


.. _try:
.. _except:
.. _except_star:
.. _finally:

The :keyword:`!try` statement
Comment thread
CAM-Gerlach marked this conversation as resolved.
=============================
Expand Down Expand Up @@ -231,8 +229,17 @@ for a group of statements:
try3_stmt: "try" ":" `suite`
: "finally" ":" `suite`

Additional information on exceptions can be found in section :ref:`exceptions`,
and information on using the :keyword:`raise` statement to generate exceptions
may be found in section :ref:`raise`.

The :keyword:`except` clause(s) specify one or more exception handlers. When no

.. _except:

:keyword:`!except` clause
-------------------------

The :keyword:`!except` clause(s) specify one or more exception handlers. When no
exception occurs in the :keyword:`try` clause, no exception handler is executed.
When an exception occurs in the :keyword:`!try` suite, a search for an exception
handler is started. This search inspects the except clauses in turn until one
Expand Down Expand Up @@ -312,11 +319,17 @@ when leaving an exception handler::
>>> print(sys.exc_info())
(None, None, None)


.. index::
keyword: except_star

The :keyword:`except*<except_star>` clause(s) are used for handling
:exc:`ExceptionGroup`\ s. The exception type for matching is interpreted as in
.. _except_star:

:keyword:`!except*` clause
--------------------------

The :keyword:`except* <except_star>` clause(s) are used for handling
Comment thread
CAM-Gerlach marked this conversation as resolved.
Outdated
:exc:`ExceptionGroup`\s. The exception type for matching is interpreted as in
the case of :keyword:`except`, but in the case of exception groups we can have
partial matches when the type matches some of the exceptions in the group.
This means that multiple except* clauses can execute, each handling part of
Comment thread
CAM-Gerlach marked this conversation as resolved.
Outdated
Expand Down Expand Up @@ -359,14 +372,25 @@ one except* clause, the first that matches it. ::
statement: break
statement: continue

.. _except_else:

:keyword:`!else` clause
-----------------------

The optional :keyword:`!else` clause is executed if the control flow leaves the
:keyword:`try` suite, no exception was raised, and no :keyword:`return`,
:keyword:`continue`, or :keyword:`break` statement was executed. Exceptions in
the :keyword:`!else` clause are not handled by the preceding :keyword:`except`
clauses.


.. index:: keyword: finally

.. _finally:

:keyword:`!finally` clause
--------------------------

If :keyword:`finally` is present, it specifies a 'cleanup' handler. The
:keyword:`try` clause is executed, including any :keyword:`except` and
:keyword:`!else` clauses. If an exception occurs in any of the clauses and is
Expand Down Expand Up @@ -412,10 +436,6 @@ always be the last one executed::
>>> foo()
'finally'

Additional information on exceptions can be found in section :ref:`exceptions`,
and information on using the :keyword:`raise` statement to generate exceptions
may be found in section :ref:`raise`.

.. versionchanged:: 3.8
Prior to Python 3.8, a :keyword:`continue` statement was illegal in the
:keyword:`finally` clause due to a problem with the implementation.
Expand Down