Skip to content

Commit

Permalink
add example using leaf_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Mar 1, 2021
1 parent 30d36f6 commit 992b107
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pep-0654.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,53 @@ recursively, as follows:
tbs.pop()
We can then process the full tracebacks of the leaf exceptions:

.. code-block::
>>> import traceback
>>>
>>> def g(v):
... try:
... raise ValueError(v)
... except Exception as e:
... return e
...
>>> def f():
... raise ExceptionGroup("eg", [g(1), g(2)])
...
>>> try:
... f()
... except BaseException as e:
... eg = e
...
>>> for (i, (exc, tbs)) in enumerate(leaf_generator(eg)):
... print(f"\n>>> Exception #{i+1}:")
... traceback.print_exception(exc)
... print(f"The complete traceback for Exception #{i+1}:")
... for tb in tbs:
... traceback.print_tb(tb)
...
>>> Exception #1:
Traceback (most recent call last):
File "<stdin>", line 3, in g
ValueError: 1
The complete traceback for Exception #1
File "<stdin>", line 2, in <module>
File "<stdin>", line 2, in f
File "<stdin>", line 3, in g
>>> Exception #2:
Traceback (most recent call last):
File "<stdin>", line 3, in g
ValueError: 2
The complete traceback for Exception #2:
File "<stdin>", line 2, in <module>
File "<stdin>", line 2, in f
File "<stdin>", line 3, in g
>>>
except*
-------

Expand Down

0 comments on commit 992b107

Please sign in to comment.