python - 如果第一个元素是异常,为什么提升元组有效?

标签 python python-2.7 exception tuples

我很难弄清楚这一点,这是关于在 Python 2.7 中引发异常时可能会犯的错误:

try:
  raise [1, 2, 3, 4]
except Exception as ex:
  print ex

这里的消息是“异常必须是旧式类或派生自BaseException,而不是列表”-这部分没问题,但是当我将其更改为元组时,我感到困惑:

try:
  raise (1, 2, 3, 4)
except Exception as ex:
  print ex

这里的信息是“异常必须是旧式类或派生自 BaseException,而不是 int”——为什么它被解释为引发 int,而不是元组?

此外:

try:
  raise (Exception, 'a message')
except Exception as ex:
  print ex

这里我们实际上是在引发一个异常(与前面的例子相比,我们正在引发一个 int 的行为一致) - 我简要地认为这只是一种替代方式:

try:
  raise Exception, 'a message'
except Exception as ex:
  print ex

但在这种情况下,“一条消息”被传递给 Exceptions ctor(如 docs.python.org 中所述)

有人可以解释第 2 和第 3 种情况,并可能指出我在解释器中负责此的代码吗?

最佳答案

作为 documented in the Python 2 referenceraise 语句最多需要 3 个表达式来创建引发的异常:

raise_stmt ::= "raise" [expression ["," expression ["," expression]]]

如果第一个表达式是元组,python 将递归地“解包”元组,获取第一个元素,直到找到元组以外的其他元素。此行为已从 Python 3 中移除(参见 PEP 3109)。以下是合法的:

>>> raise ((Exception, 'ignored'), 'ignored'), 'something', None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: something

文档更详细地解释了其余部分,但 raise 语句期望第一个值是 Exception 类,第二个值被视为异常(消息)的值,第三个值是回溯。如果缺少后两个值,Python 会为 None 填充。

如果第一个值为 instance,则第二个值必须为 None:

>>> raise Exception('something'), 'something', None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: instance exception may not have a separate value

如果使用超过 3 项的元组,则会引发语法错误:

>>> raise Exception, 'something', None, None
  File "<stdin>", line 1
    raise Exception, 'something', None, None
                                      ^
SyntaxError: invalid syntax

但是,在您的情况下,您既没有提出类也没有提出实例,因此 Python 首先发现是不正确的;如果我使用字符串,它也会提示:

>>> raise 'not an exception', 'something', None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: exceptions must be old-style classes or derived from BaseException, not str

正确的语法当然是:

>>> raise Exception, 'something', None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: something

关于python - 如果第一个元素是异常,为什么提升元组有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9836756/

相关文章:

python - 在Python中将S3数据并行/异步下载到EC2中?

python - 使用 Cloud Scheduler 的 HTTP 触发 Cloud Function

python - 修改 python 脚本以在目录中的每个文件上运行

python - 使用 python 在 azure 上将基础设施作为代码

.net - WPF:为什么这会导致堆栈溢出异常?

java - 尝试排序时 Java 中出现异常错误

java - 从 Callable 返回的本地类的序列化

python - 如何在 python session 中获取 duckdb 可见的类表对象列表?

Python:在当前目录及其所有父目录中搜索文件

python - 替换列表中子列表中所有出现的项目