@@ -851,6 +851,78 @@ The following exceptions are used as warning categories; see the
851851 .. versionadded :: 3.2
852852
853853
854+ Exception groups
855+ ----------------
856+
857+ The following are used when it is necessary to raise multiple unrelated
858+ exceptions. They are part of the exception hierarchy so they can be
859+ handled with :keyword: `except ` like all other exceptions. In addition,
860+ they are recognised by :keyword: `except*<except_star> `, which matches
861+ their subgroups based on the types of the contained exceptions.
862+
863+ .. exception :: ExceptionGroup(msg, excs)
864+ .. exception :: BaseExceptionGroup(msg, excs)
865+
866+ Both of these exception types wrap the exceptions in the sequence ``excs ``.
867+ The ``msg `` parameter must be a string. The difference between the two
868+ classes is that :exc: `BaseExceptionGroup ` extends :exc: `BaseException ` and
869+ it can wrap any exception, while :exc: `ExceptionGroup ` extends :exc: `Exception `
870+ and it can only wrap subclasses of :exc: `Exception `. This design is so that
871+ ``except Exception `` catches an :exc: `ExceptionGroup ` but not
872+ :exc: `BaseExceptionGroup `.
873+
874+ The :exc: `BaseExceptionGroup ` constructor returns an :exc: `ExceptionGroup `
875+ rather than a :exc: `BaseExceptionGroup ` if all contained exceptions are
876+ :exc: `Exception ` instances, so it can be used to make the selection
877+ automatic. The :exc: `ExceptionGroup ` constructor, on the other hand,
878+ raises a :exc: `TypeError ` if any contained exception is not an
879+ :exc: `Exception ` subclass.
880+
881+ .. method :: subgroup(condition)
882+
883+ Returns an exception group that contains only the exceptions from the
884+ current group that match *condition *, or ``None `` if the result is empty.
885+
886+ The condition can be either a function that accepts an exception and returns
887+ true for those that should be in the subgroup, or it can be an exception type
888+ or a tuple of exception types, which is used to check for a match using the
889+ same check that is used in an ``except `` clause.
890+
891+ The nesting structure of the current exception is preserved in the result,
892+ as are the values of its :attr: `message `, :attr: `__traceback__ `,
893+ :attr: `__cause__ `, :attr: `__context__ ` and :attr: `__note__ ` fields.
894+ Empty nested groups are omitted from the result.
895+
896+ The condition is checked for all exceptions in the nested exception group,
897+ including the top-level and any nested exception groups. If the condition is
898+ true for such an exception group, it is included in the result in full.
899+
900+ .. method :: split(condition)
901+
902+ Like :meth: `subgroup `, but returns the pair ``(match, rest) `` where ``match ``
903+ is ``subgroup(condition) `` and ``rest `` is the remaining non-matching
904+ part.
905+
906+ .. method :: derive(excs)
907+
908+ Returns an exception group with the same :attr: `message `,
909+ :attr: `__traceback__ `, :attr: `__cause__ `, :attr: `__context__ `
910+ and :attr: `__note__ ` but which wraps the exceptions in ``excs ``.
911+
912+ This method is used by :meth: `subgroup ` and :meth: `split `. A
913+ subclass needs to override it in order to make :meth: `subgroup `
914+ and :meth: `split ` return instances of the subclass rather
915+ than :exc: `ExceptionGroup `. ::
916+
917+ >>> class MyGroup(ExceptionGroup):
918+ ... def derive(self, exc):
919+ ... return MyGroup(self.message, exc)
920+ ...
921+ >>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
922+ (MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
923+
924+ .. versionadded :: 3.11
925+
854926
855927Exception hierarchy
856928-------------------
0 commit comments