Question presented to candidate: "Python 3.11 added ExceptionGroup and a new except* syntax. What problem do they solve, and how does except* behave differently from a normal except when a group contains several different errors?"
What a strong answer should cover:
- The problem: concurrent tasks or bulk validation can fail in several unrelated ways at once, and a normal exception can only report one;
ExceptionGroup(message, [exc, ...])carries them all (PEP 654, Python 3.11) except* Tmatches the leaves of the group by type, at any nesting depth, and binds a new group holding only the matching leaves; severalexcept*clauses can run for one group- Leaves that no clause matches are re-raised as a group, preserving the nesting; a plain exception caught by
except*arrives wrapped in a group - Rules: you cannot mix
exceptandexcept*on onetry;break,continueandreturnare not allowed insideexcept*;except* ExceptionGroupis aTypeError ExceptionGroupis anException;BaseExceptionGroupis used when a leaf is aBaseExceptionsuch asKeyboardInterrupt- Where groups come from in practice:
asyncio.TaskGroup(3.11), and theexceptiongroupbackport for older Pythons
Code / implementation expected: Yes β raise a group of two or three different errors and handle them with separate except* clauses, showing what each handler receives.