Visitar URL original
gh-89770: [PEP-678] documentation on exception notes by iritkatriel · Pull Request #30441 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update for accepted version of the PEP
  • Loading branch information
iritkatriel committed Apr 18, 2022
commit 729c10c336c20c8d5df3434b591238aa9e6f4fd2
8 changes: 4 additions & 4 deletions Doc/library/traceback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ capture data for later printing in a lightweight fashion.

The ``__suppress_context__`` value from the original exception.

.. attribute:: __note__
.. attribute:: __notes__

The ``__note__`` value from the original exception, which is
string or ``None``. If it is not ``None`` is it formatted in
the traceback after the exception string.
The ``__notes__`` value from the original exception, or ``None``
if the exception does not have any notes. If it is not ``None``
is it formatted in the traceback after the exception string.

.. versionadded:: 3.11

Expand Down
12 changes: 7 additions & 5 deletions Doc/tutorial/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -558,20 +558,22 @@ Enriching Exceptions with Notes
When an exception is created in order to be raised, it is usually initialized
with information that describes the error that has occurred. There are cases
where it is useful to add information after the exception was caught. For this
purpose, exceptions have a ``__note__`` attribute that can be assigned a string or
``None`` value (``None`` by default). If it is a string, it is included in the
formatted tracebacks after the exception. ::
purpose, exceptions have a method ``add_node(note)`` that accepts a string and
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
adds it to the exception's notes list. The standard traceback includes all
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
notes, in the order they were added, after the exception. ::

>>> try:
... raise TypeError('bad type')
... except Exception as e:
... e.__note__ = 'Add some information'
... e.add_note('Add some information')
... e.add_note('Add some more information')
... raise
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: bad type
Add some information
Add some more information
>>>

For example, when collecting exceptions into an exception group, we may want
Expand All @@ -586,7 +588,7 @@ exception in the group has a note indicating when this error has occurred. ::
... try:
... f()
... except Exception as e:
... e.__note__ = f'Happened in Iteration {i+1}'
... e.add_note(f'Happened in Iteration {i+1}')
... excs.append(e)
...
>>> raise ExceptionGroup('We have some problems', excs)
Expand Down