Was bedeutet dieser Code?
Code BaseExceptionGroup · Ein von Python gemeldetes Problem.
Übersetzte Erklärung
Beide Ausnahmetypen umschließen die Ausnahmen in der Sequenz excs. Der msg-Parameter muss eine Zeichenfolge sein. Der Unterschied zwischen den beiden Klassen besteht darin, dass BaseExceptionGroup BaseException erweitert und jede Ausnahme umschließen kann, während ExceptionGroup Exception erweitert und nur Unterklassen von Exception umschließen kann. Dieses Design ist so konzipiert, dass außer Exception eine ExceptionGroup abfängt, nicht jedoch BaseExceptionGroup.
Der BaseExceptionGroup-Konstruktor gibt eine ExceptionGroup statt einer BaseExceptionGroup zurück, wenn alle enthaltenen Ausnahmen Ausnahmeinstanzen sind, sodass er verwendet werden kann, um die Auswahl automatisch vorzunehmen. Der ExceptionGroup-Konstruktor hingegen löst einen TypeError aus, wenn eine enthaltene Ausnahme keine Exception-Unterklasse ist.
Ausnahmegruppen sind hinsichtlich des Typs ihrer enthaltenen Ausnahmen generisch.
Beachten Sie, dass BaseExceptionGroup __new__() definiert. Daher müssen Unterklassen, die eine andere Konstruktorsignatur benötigen, diese überschreiben und nicht __init__(). Im Folgenden wird beispielsweise eine Unterklasse einer Ausnahmegruppe definiert, die einen Exit_code akzeptiert und daraus die Nachricht der Gruppe erstellt.
Wie ExceptionGroup kann jede Unterklasse von BaseExceptionGroup, die auch eine Unterklasse von Exception ist, nur Instanzen von Exception umschließen.
Google Translate · Dies ist eine automatische Übersetzung. Prüfen Sie Fachbegriffe und anwendbare Bedingungen im englischen Original.
Offizielle Erklärung · Originaltext
Both of these exception types wrap the exceptions in the sequence excs. The msg parameter must be a string. The difference between the two classes is that BaseExceptionGroup extends BaseException and it can wrap any exception, while ExceptionGroup extends Exception and it can only wrap subclasses of Exception. This design is so that except Exception catches an ExceptionGroup but not BaseExceptionGroup.
The BaseExceptionGroup constructor returns an ExceptionGroup rather than a BaseExceptionGroup if all contained exceptions are Exception instances, so it can be used to make the selection automatic. The ExceptionGroup constructor, on the other hand, raises a TypeError if any contained exception is not an Exception subclass.
Exception groups are generic over the type of their contained exceptions.
Note that BaseExceptionGroup defines __new__(), so subclasses that need a different constructor signature need to override that rather than __init__(). For example, the following defines an exception group subclass which accepts an exit_code and constructs the group’s message from it.
Like ExceptionGroup, any subclass of BaseExceptionGroup which is also a subclass of Exception can only wrap instances of Exception.