鱼C论坛

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

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

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

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

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

x
原文:

  1. Help on class BaseExceptionGroup in module builtins:

  2. class BaseExceptionGroup(BaseException)
  3. |  A combination of multiple unrelated exceptions.
  4. |
  5. |  Method resolution order:
  6. |      BaseExceptionGroup
  7. |      BaseException
  8. |      object
  9. |
  10. |  Built-in subclasses:
  11. |      ExceptionGroup
  12. |
  13. |  Methods defined here:
  14. |
  15. |  __init__(self, /, *args, **kwargs)
  16. |      Initialize self.  See help(type(self)) for accurate signature.
  17. |
  18. |  __str__(self, /)
  19. |      Return str(self).
  20. |
  21. |  derive(self, object, /)
  22. |
  23. |  split(self, object, /)
  24. |
  25. |  subgroup(self, object, /)
  26. |
  27. |  ----------------------------------------------------------------------
  28. |  Class methods defined here:
  29. |
  30. |  __class_getitem__(object, /)
  31. |      See PEP 585
  32. |
  33. |  ----------------------------------------------------------------------
  34. |  Static methods defined here:
  35. |
  36. |  __new__(*args, **kwargs)
  37. |      Create and return a new object.  See help(type) for accurate signature.
  38. |
  39. |  ----------------------------------------------------------------------
  40. |  Data descriptors defined here:
  41. |
  42. |  exceptions
  43. |      nested exceptions
  44. |
  45. |  message
  46. |      exception message
  47. |
  48. |  ----------------------------------------------------------------------
  49. |  Methods inherited from BaseException:
  50. |
  51. |  __getattribute__(self, name, /)
  52. |      Return getattr(self, name).
  53. |
  54. |  __reduce__(self, /)
  55. |      Helper for pickle.
  56. |
  57. |  __repr__(self, /)
  58. |      Return repr(self).
  59. |
  60. |  __setstate__(self, object, /)
  61. |
  62. |  add_note(self, object, /)
  63. |      Exception.add_note(note) --
  64. |      add a note to the exception
  65. |
  66. |  with_traceback(self, object, /)
  67. |      Exception.with_traceback(tb) --
  68. |      set self.__traceback__ to tb and return self.
  69. |
  70. |  ----------------------------------------------------------------------
  71. |  Data descriptors inherited from BaseException:
  72. |
  73. |  __cause__
  74. |      exception cause
  75. |
  76. |  __context__
  77. |      exception context
  78. |
  79. |  __dict__
  80. |
  81. |  __suppress_context__
  82. |
  83. |  __traceback__
  84. |
  85. |  args
复制代码

翻译:

  1. 模块内置类中 BaseExceptionGroup 的帮助:

  2. 类 BaseExceptionGroup(BaseException)
  3. | 多个无关异常的组合。
  4. |
  5. | 方法解决顺序:
  6. | BaseExceptionGroup
  7. | BaseException
  8. | 对象
  9. |
  10. | 内置子类:
  11. | 异常组
  12. |
  13. | 这里定义的方法:
  14. |
  15. | __init__(self, /, *args, **kwargs)
  16. | 初始化自身。 有关准确的签名,请参见 help(type(self)) 。
  17. |
  18. | __str__(self, /)
  19. | 返回 str(self)。
  20. |
  21. | 派生(self, object, /)
  22. |
  23. | split(self, object, /)
  24. |
  25. | subgroup(self, object, /)   
  26. |
  27. | ----------------------------------------------------------------------
  28. | 这里定义的类方法:
  29. |
  30. | __class_getitem__(object, /)
  31. | 参见 PEP 585
  32. |
  33. | ----------------------------------------------------------------------
  34. | 此处定义的静态方法:
  35. |
  36. | __new__(*args, **kwargs)
  37. | 创建并返回一个新对象。 有关准确的签名,请参见 help(type)。
  38. |
  39. | ----------------------------------------------------------------------
  40. | 此处定义的数据描述符:
  41. |
  42. | 异常
  43. | 嵌套异常
  44. |
  45. | 信息
  46. | 异常消息
  47. |
  48. | ----------------------------------------------------------------------
  49. | 继承自 BaseException 的方法:
  50. |
  51. | __getattribute__(self, name, /)
  52. | 返回 getattr(self,name)。
  53. |
  54. | __reduce__(self, /)
  55. | pickle 的辅助函数。
  56. |
  57. | 返回 __repr__(self, /)
  58. | 返回 repr(self)。
  59. |
  60. | __setstate__(self, object, /)
  61. |
  62. | add_note(self, object, /)
  63. | Exception.add_note(note) --
  64. | 为异常添加注释
  65. |
  66. | with_traceback(self, object, /)
  67. | Exception.with_traceback(tb) -- | 为异常添加注释。
  68. | 将 self.__traceback__ 设为 tb 并返回 self。
  69. |
  70. | ----------------------------------------------------------------------
  71. | 从 BaseException 继承的数据描述符:
  72. |
  73. | __cause__
  74. | 异常原因
  75. |
  76. | __context__
  77. | 异常上下文
  78. |
  79. | __dict__
  80. |
  81. | __suppress_context__
  82. |
  83. | __traceback__
  84. |
  85. | args
复制代码


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

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

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

创建异常组:

  1. BaseExceptionGroup(message, exceptions)
复制代码


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

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

方法:

subgroup

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

  1. 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:其他可选的关键字参数。

示例:


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

  4. def create_exception_group():
  5.     try:
  6.         addition()
  7.     except Exception as e:
  8.         raise BaseExceptionGroup("Multiple exceptions occurred", [e, ValueError("Another error")])

  9. try:
  10.     create_exception_group()
  11. except BaseExceptionGroup as beg:
  12.     for exc in beg.exceptions:
  13.         print(f"Caught exception: {exc}")
复制代码

addition函数会引发一个TypeError,然后create_exception_group函数将这个异常和其他异常一起包装在一个BaseExceptionGroup中抛出,并在外层的except块中捕获和处理这些异常.
只能说这么多了,没啥水平
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

原文:

小甲鱼最新课程 -> https://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 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-1-27 19:26:11 | 显示全部楼层
加油,多做一些
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-6 03:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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