Table of Contents

Class BaseExceptionGroup

Namespace
NumSharp
Assembly
NumSharp.dll

Python's BaseExceptionGroup: several unrelated exceptions raised together (Python 3.11+, PEP 654), with the operations Python's except* is built on — subgroup(params Type[]), split(params Type[]) and the derive(IEnumerable<Exception>) hook. Its Message is Python's str(): message (2 sub-exceptions).

public class BaseExceptionGroup : AggregateException, ISerializable, INumSharpException
Inheritance
BaseExceptionGroup
Implements
Derived
Inherited Members

Remarks

It derives from .NET's AggregateException, the exception with the same meaning, so its members are InnerExceptions (also exceptions) and a catch (AggregateException) catches it. Footgun inherited from that base: InnerException is the FIRST member, not Python's __cause__; a group has no separate cause, so the standard (message, innerException) constructor builds a group of that one member, as AggregateException's does.

Python's BaseExceptionGroup(message, excs) returns an ExceptionGroup when every member is an Exception; a C# constructor cannot return a subclass, so new BaseExceptionGroup(...) always builds this class, and Create(string, IEnumerable<Exception>) is Python's call (it picks the class as CPython does). KeyboardInterrupt, SystemExit, GeneratorExit and a group that is not an ExceptionGroup are Python's BaseException-only members; every other .NET exception counts as an Exception.

NumSharp never raises a group; the classes exist so a port of code that raises, splits or catches groups compiles and behaves as in Python.

Constructors

BaseExceptionGroup(string, IEnumerable<Exception>)

Python's BaseExceptionGroup(message, exceptions), building this class whatever the members are (see Create(string, IEnumerable<Exception>) for Python's class choice).

public BaseExceptionGroup(string message, IEnumerable<Exception> exceptions)

Parameters

message string

The group's message (Python's message).

exceptions IEnumerable<Exception>

The members, in order; enumerated once.

Exceptions

TypeError

message is null (CPython: BaseExceptionGroup.new() argument 1 must be str, not None), or exceptions is null (second argument (exceptions) must be a sequence).

ValueError

exceptions is empty (second argument (exceptions) must be a non-empty sequence) or holds a null (Item 1 of second argument (exceptions) is not an exception).

BaseExceptionGroup(string, Exception)

.NET's standard (message, innerException) constructor every NumSharp exception has, with AggregateException's meaning: innerException becomes the group's one member (Python's BaseExceptionGroup(message, [innerException])), reading message (1 sub-exception).

public BaseExceptionGroup(string message, Exception innerException)

Parameters

message string

The group's message (Python's message).

innerException Exception

The group's only member.

Remarks

A group has no separate cause (see the class remarks): the exception is a MEMBER, so subgroup(params Type[]) and split(params Type[]) match it. Footgun: new BaseExceptionGroup("m", null) does not compile (both two-argument constructors accept a null); cast it to IEnumerable<Exception> for Python's BaseExceptionGroup("m", None).

Exceptions

TypeError

message is null.

ValueError

innerException is null (CPython's Item 0 of second argument (exceptions) is not an exception).

Properties

Message

Python's str() of the group: message (1 sub-exception) / message (n sub-exceptions), replacing AggregateException's text (which appends every member's message).

public override string Message { get; }

Property Value

string

exceptions

The members, in order (Python's exceptions tuple): the same collection as InnerExceptions.

public ReadOnlyCollection<Exception> exceptions { get; }

Property Value

ReadOnlyCollection<Exception>

message

The group's message as given (Python's message), without the count Message adds.

public string message { get; }

Property Value

string

Methods

Create(string, IEnumerable<Exception>)

Python's BaseExceptionGroup(message, exceptions) call: an ExceptionGroup when every member is a Python Exception, else a BaseExceptionGroup.

public static BaseExceptionGroup Create(string message, IEnumerable<Exception> exceptions)

Parameters

message string

The group's message.

exceptions IEnumerable<Exception>

The members, in order; enumerated once.

Returns

BaseExceptionGroup

The new group.

Exceptions

TypeError

A null message or member sequence.

ValueError

An empty member sequence, or a null member.

derive(IEnumerable<Exception>)

Python's derive(excs): a new group with this group's message and excs as members. subgroup(params Type[]) and split(params Type[]) build every part through it, so a subclass overrides it to keep its own class and fields in those parts (Python's documented extension point).

public virtual BaseExceptionGroup derive(IEnumerable<Exception> excs)

Parameters

excs IEnumerable<Exception>

The members of the new group.

Returns

BaseExceptionGroup

The new group: by default Create(string, IEnumerable<Exception>)'s choice, a plain ExceptionGroup or BaseExceptionGroup, NOT this group's subclass (as in Python).

Exceptions

TypeError

A null member sequence.

ValueError

An empty member sequence, or a null member.

split(Func<Exception, bool>)

Python's split(condition) with a predicate: (match, rest) by condition, as split(params Type[]) does by type.

public (BaseExceptionGroup match, BaseExceptionGroup rest) split(Func<Exception, bool> condition)

Parameters

condition Func<Exception, bool>

The predicate, called on this group, then on each member and nested group.

Returns

(BaseExceptionGroup match, BaseExceptionGroup rest)

The matching and the remaining parts.

Exceptions

TypeError

condition is null.

split(params Type[])

Python's split(condition) with exception types: (match, rest), the members that are instances of any of types and the ones that are not, each a group with this group's structure or null when empty.

public (BaseExceptionGroup match, BaseExceptionGroup rest) split(params Type[] types)

Parameters

types Type[]

The exception types; none matches nothing.

Returns

(BaseExceptionGroup match, BaseExceptionGroup rest)

The matching and the remaining parts.

Remarks

Each part is built by derive(IEnumerable<Exception>) and takes this group's Data (an independent copy) and its stack trace, as CPython copies notes and traceback.

Exceptions

TypeError

A null or non-exception type.

subgroup(Func<Exception, bool>)

Python's subgroup(condition) with a predicate: the members (and nested groups) for which condition returns true, or null when none does. The predicate sees this group first, and a group it accepts is kept whole.

public BaseExceptionGroup subgroup(Func<Exception, bool> condition)

Parameters

condition Func<Exception, bool>

The predicate, called on this group, then on each member and nested group.

Returns

BaseExceptionGroup

The matching subgroup, this group, or null.

Exceptions

TypeError

condition is null (CPython: expected a function, exception type or tuple of exception types).

subgroup(params Type[])

Python's subgroup(condition) with exception types: the members that are instances of any of types, nested groups kept as nested (sub)groups, or null when none is. The whole group is returned as is when it matches itself.

public BaseExceptionGroup subgroup(params Type[] types)

Parameters

types Type[]

The exception types (Python's type or tuple of types); none matches nothing.

Returns

BaseExceptionGroup

The matching subgroup, this group, or null.

Exceptions

TypeError

types is null, or one of them is null or not an exception type (CPython: expected a function, exception type or tuple of exception types).