鱼C论坛

 找回密码
 立即注册
查看: 105|回复: 3

[技术交流] BaseExceptionGroup-PythonBIF(5)

[复制链接]
发表于 2025-1-27 14:53:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
原文:
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", [e, ValueError("Another error")])

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块中捕获和处理这些异常.
只能说这么多了,没啥水平
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2025-1-27 15:08:30 | 显示全部楼层
FishC 发表于 2025-1-26 20:54
BaseExceptionGroup-PythonBIF(5)

原文:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-1-27 15:08:36 | 显示全部楼层
zyx2012 发表于 2025-01-26 21:08
FishC 发表于 2025-1-26 20:54
BaseExceptionGroup-P ...


表情仅适用于Discuz! 论坛中的表情代码。请在论坛中使用该表情。

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-1-27 19:26:11 | 显示全部楼层
加油,多做一些
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-24 02:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表