diff --git a/pep-0654.rst b/pep-0654.rst index 7677f4bbc03..0dd5f1029c9 100644 --- a/pep-0654.rst +++ b/pep-0654.rst @@ -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 "", line 3, in g + ValueError: 1 + The complete traceback for Exception #1 + File "", line 2, in + File "", line 2, in f + File "", line 3, in g + + >>> Exception #2: + Traceback (most recent call last): + File "", line 3, in g + ValueError: 2 + The complete traceback for Exception #2: + File "", line 2, in + File "", line 2, in f + File "", line 3, in g + >>> + except* -------