I’m interested in the intended use cases of Exception Groups (PEP 654) and how they would look like in user code. Reading the part about
except* it says
[…] In other words, a single exception group can cause several except* clauses to execute, but each such clause executes at most once (for all matching exceptions from the group) and each exception is either handled by exactly one clause (the first one that matches its type) or is reraised at the end. [emphasis mine]
later followed by an example
try: low_level_os_operation() except *OSError as eg: for e in eg.exceptions: print(type(e).__name__)
I wondered what the use case for exposing (part of) the exception group as a collection is (i.e.
*OSError as eg)? To me the most obvious thing to do if multiple concurrent exceptions were raised, would be to do something for each exception like in the example above (i.e.
for e in eg.exceptions).
The rejected ideas section mentions making exception groups iterable but I think this concerns the exception group with multiple different errors as a whole (e.g.
ExceptionGroup('one', [TypeError(1), ValueError(1), ExceptionGroup('two', ...)]))?
Has the idea been considered to focus the syntax for individual
except * clauses around iteration? So instead of
try: async with trio.open_nursery() as nursery: for operation in list_of_operations: nursery.start_soon(operation) except *OSError as eg: for e in eg.exceptions: print(type(e).__name__) except for *SomeOtherError as eg: for e in eg.exceptions: ...
maybe something like:
try: async with trio.open_nursery() as nursery: for operation in list_of_operations: nursery.start_soon(operation) except for OSError as error: # yields all OSErrors print(type(error).__name__) except for SomeOtherError as error: ...