Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to turn off exception auto-correlation context by python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how python turns off the abnormal automatic context. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

How to turn off the automatic context of exception association

When you are dealing with an exception, when you throw another exception due to improper handling or other problems, the exception thrown will also carry the original exception information.

Just like this.

Try: print (1 / 0) except Exception as exc: raise RuntimeError ("Something bad happened")

You can see two abnormal messages from the output

Traceback (most recent call last): File "demo.py", line 2, in print (1 / 0) ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "demo.py", line 4, in raise RuntimeError ("Something bad happened") RuntimeError: Something bad happened

If an exception is thrown in an exception handler or finally block, by default, the exception mechanism implicitly appends the previous exception to the _ _ context__ attribute of the new exception. This is the automatic association exception context enabled by Python by default.

If you want to control the context yourself, you can add the from keyword (there is a restriction in the from syntax that the second expression must be another exception class or instance.) To indicate which exception your new exception is directly caused by

Try: print (1 / 0) except Exception as exc: raise RuntimeError ("Something bad happened") from exc

The output is as follows

Traceback (most recent call last): File "demo.py", line 2, in print (1 / 0) ZeroDivisionError: division by zero The above exception was the direct cause of the following exception: Traceback (most recent call last): File "demo.py", line 4, in raise RuntimeError ("Something bad happened") from exc RuntimeError: Something bad happened

Of course, you can also set the context _ _ context__ property for the exception through the with_traceback () method, which can also better display the exception information in traceback.

Try: print (1 / 0) except Exception as exc: raise RuntimeError (bad thing) .with_traceback (exc)

Finally, what if I want to completely turn off this mechanism for automatically associating exception contexts? Is there anything I can do?

You can use raise...from None, and from the following example, there are no original exceptions

$cat demo.py try: print (1 / 0) except Exception as exc: raise RuntimeError ("Something bad happened") from None $$python demo.py Traceback (most recent call last): File "demo.py", line 4, in raise RuntimeError ("Something bad happened") from None RuntimeError: Something bad happened (PythonCodingTime) this article on "how python shuts down the exception auto-correlation context" ends here. I hope the above can be of some help to you. So that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report