Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-95975: Move except/*/finally ref labels to more precise locations #95976

Merged
merged 7 commits into from
Oct 2, 2022
34 changes: 27 additions & 7 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
CAM-Gerlach marked this conversation as resolved.
Show resolved Hide resolved
=============================
Expand Down Expand Up @@ -231,6 +229,15 @@ 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`.


.. _except:

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

The :keyword:`except` clause(s) specify one or more exception handlers. When no
CAM-Gerlach marked this conversation as resolved.
Show resolved Hide resolved
exception occurs in the :keyword:`try` clause, no exception handler is executed.
Expand Down Expand Up @@ -312,9 +319,15 @@ when leaving an exception handler::
>>> print(sys.exc_info())
(None, None, None)


.. index::
keyword: except_star

.. _except_star:

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

The :keyword:`except*<except_star>` clause(s) are used for handling
:exc:`ExceptionGroup`\ s. The exception type for matching is interpreted as in
CAM-Gerlach marked this conversation as resolved.
Show resolved Hide resolved
the case of :keyword:`except`, but in the case of exception groups we can have
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