gpt4 book ai didi

Python "raise from"用法

转载 作者:IT老高 更新时间:2023-10-28 12:30:58 25 4
gpt4 key购买 nike

Python中的raiseraise from有什么区别?

try:
raise ValueError
except Exception as e:
raise IndexError

产生

Traceback (most recent call last):
File "tmp.py", line 2, in <module>
raise ValueError
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "tmp.py", line 4, in <module>
raise IndexError
IndexError

try:
raise ValueError
except Exception as e:
raise IndexError from e

产生

Traceback (most recent call last):
File "tmp.py", line 2, in <module>
raise ValueError
ValueError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "tmp.py", line 4, in <module>
raise IndexError from e
IndexError

最佳答案

不同的是,当你使用from时,__cause__属性被设置,并且消息指出异常是直接造成的。如果省略 from 则不会设置 __cause__,但也可以设置 __context__ 属性,并且然后,回溯将上下文显示为在处理其他发生的事情期间

如果您在异常处理程序中使用 raise,则会设置 __context__;如果您在其他任何地方使用 raise 也没有设置 __context__

如果设置了 __cause__,则异常也会设置 __suppress_context__ = True 标志;当 __suppress_context__ 设置为 True 时,打印回溯时会忽略 __context__

当您从想要显示上下文的异常处理程序引发时(不希望在处理另一个异常发生期间消息),然后使用 raise ... from None__suppress_context__ 设置为 True

换句话说,Python 为异常设置了一个 context,这样您就可以自省(introspection)引发异常的位置,让您查看是否有另一个异常被它替换。您还可以将 cause 添加到异常中,使回溯显式地显示另一个异常(使用不同的措辞),并忽略上下文(但在调试时仍然可以自省(introspection))。使用 raise ... from None 可以抑制正在打印的上下文。

raise statement documenation :

The from clause is used for exception chaining: if given, the second expression must be another exception class or instance, which will then be attached to the raised exception as the __cause__ attribute (which is writable). If the raised exception is not handled, both exceptions will be printed:

>>> try:
... print(1 / 0)
... except Exception as exc:
... raise RuntimeError("Something bad happened") from exc
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: int division or modulo by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "<stdin>", line 4, in <module>
RuntimeError: Something bad happened

A similar mechanism works implicitly if an exception is raised inside an exception handler or a finally clause: the previous exception is then attached as the new exception’s __context__ attribute:

>>> try:
... print(1 / 0)
... except:
... raise RuntimeError("Something bad happened")
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: int division or modulo by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 4, in <module>
RuntimeError: Something bad happened

另见 Built-in Exceptions documentation有关附加到异常的上下文和原因信息的详细信息。

关于Python "raise from"用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24752395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com