zyx2012 发表于 2025-1-27 14:53:49

BaseExceptionGroup-PythonBIF(5)

原文:

Help on class BaseExceptionGroup in module builtins:

class BaseExceptionGroup(BaseException)
|A combination of multiple unrelated exceptions.
|
|Method resolution order:
|      BaseExceptionGroup
|      BaseException
|      object
|
|Built-in subclasses:
|      ExceptionGroup
|
|Methods defined here:
|
|__init__(self, /, *args, **kwargs)
|      Initialize self.See help(type(self)) for accurate signature.
|
|__str__(self, /)
|      Return str(self).
|
|derive(self, object, /)
|
|split(self, object, /)
|
|subgroup(self, object, /)
|
|----------------------------------------------------------------------
|Class methods defined here:
|
|__class_getitem__(object, /)
|      See PEP 585
|
|----------------------------------------------------------------------
|Static methods defined here:
|
|__new__(*args, **kwargs)
|      Create and return a new object.See help(type) for accurate signature.
|
|----------------------------------------------------------------------
|Data descriptors defined here:
|
|exceptions
|      nested exceptions
|
|message
|      exception message
|
|----------------------------------------------------------------------
|Methods inherited from BaseException:
|
|__getattribute__(self, name, /)
|      Return getattr(self, name).
|
|__reduce__(self, /)
|      Helper for pickle.
|
|__repr__(self, /)
|      Return repr(self).
|
|__setstate__(self, object, /)
|
|add_note(self, object, /)
|      Exception.add_note(note) --
|      add a note to the exception
|
|with_traceback(self, object, /)
|      Exception.with_traceback(tb) --
|      set self.__traceback__ to tb and return self.
|
|----------------------------------------------------------------------
|Data descriptors inherited from BaseException:
|
|__cause__
|      exception cause
|
|__context__
|      exception context
|
|__dict__
|
|__suppress_context__
|
|__traceback__
|
|args

翻译:

模块内置类中 BaseExceptionGroup 的帮助:

类 BaseExceptionGroup(BaseException)
| 多个无关异常的组合。
|
| 方法解决顺序:
| BaseExceptionGroup
| BaseException
| 对象
|
| 内置子类:
| 异常组
|
| 这里定义的方法:
|
| __init__(self, /, *args, **kwargs)
| 初始化自身。 有关准确的签名,请参见 help(type(self)) 。
|
| __str__(self, /)
| 返回 str(self)。
|
| 派生(self, object, /)
|
| split(self, object, /)
|
| subgroup(self, object, /)   
|
| ----------------------------------------------------------------------
| 这里定义的类方法:
|
| __class_getitem__(object, /)
| 参见 PEP 585
|
| ----------------------------------------------------------------------
| 此处定义的静态方法:
|
| __new__(*args, **kwargs)
| 创建并返回一个新对象。 有关准确的签名,请参见 help(type)。
|
| ----------------------------------------------------------------------
| 此处定义的数据描述符:
|
| 异常
| 嵌套异常
|
| 信息
| 异常消息
|
| ----------------------------------------------------------------------
| 继承自 BaseException 的方法:
|
| __getattribute__(self, name, /)
| 返回 getattr(self,name)。
|
| __reduce__(self, /)
| pickle 的辅助函数。
|
| 返回 __repr__(self, /)
| 返回 repr(self)。
|
| __setstate__(self, object, /)
|
| add_note(self, object, /)
| Exception.add_note(note) --
| 为异常添加注释
|
| with_traceback(self, object, /)
| Exception.with_traceback(tb) -- | 为异常添加注释。
| 将 self.__traceback__ 设为 tb 并返回 self。
|
| ----------------------------------------------------------------------
| 从 BaseException 继承的数据描述符:
|
| __cause__
| 异常原因
|
| __context__
| 异常上下文
|
| __dict__
|
| __suppress_context__
|
| __traceback__
|
| args


BaseExceptionGroup是Python 3.11中引入的一个异常类,用于将多个异常组合在一起抛出。这个类直接继承自BaseException,而ExceptionGroup则继承自Exception和BaseExceptionGroup。

BaseExceptionGroup可以包含任意类型的异常,而ExceptionGroup只能包含Exception或其子类的实例。

在处理一系列操作时,如果遇到多个异常,可以将这些异常收集起来,然后用一个BaseExceptionGroup实例抛出。

创建异常组:

BaseExceptionGroup(message, exceptions)


message:一个字符串,用于说明

exceptions:一个序列,存储多个异常

方法:

subgroup

BaseExceptionGroup.subgroup方法用于创建一个子分组:

BaseExceptionGroup.subgroup(name, conditions, handler_func)


name:子分组的名称。

conditions:一个可选的条件列表,用于定义触发子分组的条件。

handler_func:一个可选的异常处理函数,用于处理触发子分组的异常。

derive
BaseExceptionGroup.derive方法用于创建自定义异常类:


BaseExceptionGroup.derive(Name, BaseClasses=None, ClassDict=None, **kwargs)

Name:新异常类的名称。

BaseClasses:新异常类所继承的父类,默认为BaseException。

ClassDict:新异常类的属性和方法的字典。

kwargs:其他可选的关键字参数。

示例:


def addition():
    a = 10
    print(a + 'string')# 这会引发TypeError

def create_exception_group():
    try:
      addition()
    except Exception as e:
      raise BaseExceptionGroup("Multiple exceptions occurred", )

try:
    create_exception_group()
except BaseExceptionGroup as beg:
    for exc in beg.exceptions:
      print(f"Caught exception: {exc}")

addition函数会引发一个TypeError,然后create_exception_group函数将这个异常和其他异常一起包装在一个BaseExceptionGroup中抛出,并在外层的except块中捕获和处理这些异常.
只能说这么多了,没啥水平{:10_266:}

zyx2012 发表于 2025-1-27 15:08:30

FishC 发表于 2025-1-26 20:54
BaseExceptionGroup-PythonBIF(5)

原文:


{:10_256:}

FishC 发表于 2025-1-27 15:08:36

zyx2012 发表于 2025-01-26 21:08
FishC 发表于 2025-1-26 20:54
BaseExceptionGroup-P ...

{:10_256:} 表情仅适用于Discuz! 论坛中的表情代码。请在论坛中使用该表情。

如果需要进一步了解或有其他问题,请告诉我!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

某一个“天” 发表于 2025-1-27 19:26:11

加油,多做一些
页: [1]
查看完整版本: BaseExceptionGroup-PythonBIF(5)