What is the difference between raise and raise ... from in Python?

不言
Release: 2018-12-13 11:19:33
forward
7234 people have browsed it

What this article brings to you is about the difference between raise and raise ... from in Python? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Getting Started

What is the difference between raise and raise from in Python?

try:
    print(1 / 0)
except Exception as exc:
    raise RuntimeError("Something bad happened")
Copy after login

Output:

Traceback (most recent call last):
  File "test4.py", line 2, in <module>
    print(1 / 0)
ZeropisionError: pision by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test4.py", line 4, in <module>
    raise RuntimeError("Something bad happened")
RuntimeError: Something bad happened
Copy after login

And raise from:

try:
    print(1 / 0)
except Exception as exc:
    raise RuntimeError("Something bad happened") from exc
Copy after login

Output:

Traceback (most recent call last):
  File "test4.py", line 2, in <module>
    print(1 / 0)
ZeropisionError: pision by zero

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

Traceback (most recent call last):
  File "test4.py", line 4, in <module>
    raise RuntimeError("Something bad happened") from exc
RuntimeError: Something bad happened
Copy after login

Analysis

The difference is that from will set the __cause__ attribute for the exception object to indicate who directly caused the exception.

A new exception occurs when handling an exception. When not using from, it is more likely that the new exception is not related to the exception being processed. And from can point out that the new exception is directly caused by the old exception. Such correlations between anomalies are helpful for subsequent analysis and troubleshooting of anomalies. There is a restriction in the from syntax, that is, the second expression must be another exception class or instance.

If an exception is thrown in an exception handler or finally block, the exception mechanism works implicitly by default to append the previous exception as the __context__ attribute of the new exception.

Of course, you can also set the context __context__ attribute for exceptions through the with_traceback() method, which can also better display exception information in traceback.

raise Exception("foo occurred").with_traceback(tracebackobj)
Copy after login

Disable exception association

from There is also a special usage: raise ... from None, which explicitly prohibits exception association by setting the __suppress_context__ attribute specification:

try:
    print(1 / 0)
except Exception as exc:
    raise RuntimeError("Something bad happened") from None
Copy after login

Output:

Traceback (most recent call last):
  File "test4.py", line 4, in <module>
    raise RuntimeError("Something bad happened") from None
RuntimeError: Something bad happened
Copy after login

Summary

When an exception is thrown in an exception handler or finally block, Python will set the context for the exception, which can be manually passed with_traceback () Set its context, or specify who caused the exception through from. These methods are all to obtain more friendly exception traceback information and print clear exception context. To ignore the context, you can disable automatic display of exception context by raising ... from None .

The above is the detailed content of What is the difference between raise and raise ... from in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template