except*遇不可哈希异常报错:属文档记载行为还是Bug?
问题:
except*处理不可哈希异常时触发TypeError? 以下是测试代码:
class MyException(Exception): __hash__ = None try: raise ExceptionGroup("Foo", [ MyException("Bar") ]) except* Exception: pass
按预期,except*应能捕获ExceptionGroup中的任意类型异常(也支持单独捕获任意类型异常),但实际处理ExceptionGroup时触发了未处理的TypeError,报错行号显示为模块的“-1行”:
+ Exception Group Traceback (most recent call last): | File "C:\Users\Josep\AppData\Roaming\JetBrains\PyCharmCE2022.2\scratches\scratch_62.py", line 6, in <module> | raise ExceptionGroup("Foo", [ | ExceptionGroup: Foo (1 sub-exception) +-+---------------- 1 ---------------- | MyException: Bar +------------------------------------ During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Josep\AppData\Roaming\JetBrains\PyCharmCE2022.2\scratches\scratch_62.py", line -1, in <module> TypeError: unhashable type: 'MyException'
补充测试细节:
- 将
except* Exception替换为更具体的子句(如except* ValueError或except* MyException),仍会触发相同报错; - 单独抛出
MyException并用普通except MyException捕获则完全正常。
普通except子句不关心异常是否可哈希,且在PEP-654或Python 3.11发行说明中,未找到except*有此限制的相关记载。请问这是预期行为,还是Python 3.11.0实现中的Bug?
复现环境:Windows系统下的64位Python 3.11.0
内容的提问来源于stack exchange,提问作者J E K
相关产品推荐
相关产品推荐

