raise和raise fromPython和有什么不一样?
raise
raise 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__设置no ,但也可以设置该 __context__ 属性,然后回溯显示上下文,就像 处理其他事件时一样 。
from
__cause__
__context__
__context__如果raise在异常处理程序中使用过,则设置发生的情况;如果您raise在其他任何地方使用过,则也不会__context__设置。
如果__cause__设置了a,那么__suppress_context__ = True还会在异常上设置一个标志;当__suppress_context__设置为时,在打印回溯时True将__context__忽略。
__suppress_context__ = True
__suppress_context__
True
从异常处理程序,你养的时候 不 希望显示上下文(不想 处理另一个异常发生时 ,然后用消息)raise ... from None来设置__suppress_context__到True。
raise ... from None
换句话说,Python在异常上设置了一个 上下文 ,因此您可以自省引发异常的位置,让您查看是否用它替换了另一个异常。您还可以将 原因 添加到异常中,使回溯明确地显示另一个异常(使用不同的措辞),并且上下文将被忽略(但在调试时仍可以自省)。使用raise ... from None使您可以禁止打印上下文。
请参阅raise语句documenation:
该from子句用于异常链接:如果给出,则第二个 表达式 必须是另一个异常类或实例,然后将其附加到引发的异常作为__cause__属性(可写)。如果未处理引发的异常,则将打印两个异常: >>> 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 如果在异常处理程序或finally子句中引发异常,则类似的机制会隐式工作:然后,将先前的异常附加为新异常的__context__属性: >>> 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
该from子句用于异常链接:如果给出,则第二个 表达式 必须是另一个异常类或实例,然后将其附加到引发的异常作为__cause__属性(可写)。如果未处理引发的异常,则将打印两个异常:
>>> 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
如果在异常处理程序或finally子句中引发异常,则类似的机制会隐式工作:然后,将先前的异常附加为新异常的__context__属性:
finally
>>> 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
另请参阅内置异常文档,以获取有关上下文的详细信息,并在异常中附加原因信息。