raise与raise······from

In python, if you want to manually trigger an exception, we will generally useraise

# -*- coding:utf-8 -*-
# @Author: WanMingZhu
# @Date: 2019/10/22 10:31
try:
    1 / 0
except ZeroDivisionError as e:
    raise RuntimeError("error occurred")
"""
Traceback (most recent call last):
  File "C:/Users/satori/Desktop/satori/task.py", line 5, in <module>
    1 / 0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/satori/Desktop/satori/task.py", line 7, in <module>
    raise RuntimeError("error occurred")
RuntimeError: error occurred
"""

If such abnormal setting, then During handling of the above exception, another exception occurred:, we just see Tip: when dealing with more than the exception, another exception arises. If we use a raise ..... from ... the grammar of it?

# -*- coding:utf-8 -*-
# @Author: WanMingZhu
# @Date: 2019/10/22 10:31
try:
    1 / 0
except ZeroDivisionError as e:
    raise RuntimeError("error occurred") from e
"""
Traceback (most recent call last):
  File "C:/Users/satori/Desktop/satori/task.py", line 5, in <module>
    1 / 0
ZeroDivisionError: division by zero

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

Traceback (most recent call last):
  File "C:/Users/satori/Desktop/satori/task.py", line 7, in <module>
    raise RuntimeError("error occurred") from e
RuntimeError: error occurred
"""

The above exception was the direct cause of the following exception:, Will tell us the above exceptions are the direct cause of abnormal generate below.

So we can see the difference between the two at, from an abnormality will set a __cause__ property, who represents the anomaly is a direct result. Handling of an exception there is a new exception in the case of not using from the more inclined is no correlation between the old and new abnormal abnormal, and from it is directly pointed out that the new exception is a direct result of abnormal old. In this case, it helps to analyze and troubleshoot anomalies. But from there restrictions that must be followed by an unusual class, or instance, or None

# -*- coding:utf-8 -*-
# @Author: WanMingZhu
# @Date: 2019/10/22 10:31
try:
    1 / 0
except ZeroDivisionError as e:
    raise RuntimeError("error occurred") from 123
"""
    raise RuntimeError("error occurred") from 123
TypeError: exception causes must derive from BaseException
"""
# 提示我们必须from一个BaseException
# 这个BaseException是Exception的父类,Exception继承自BaseException
# -*- coding:utf-8 -*-
# @Author: WanMingZhu
# @Date: 2019/10/22 10:31
try:
    1 / 0
except ZeroDivisionError as e:
    raise RuntimeError("error occurred") from IndexError
"""
    raise RuntimeError("error occurred") from 123
TypeError: exception causes must derive from BaseException
"""
# 这里是ZeroDivisionError,我可以手动用IndexError引发

But if I throw an exception in finally in

# -*- coding:utf-8 -*-
# @Author: WanMingZhu
# @Date: 2019/10/22 10:31
try:
    1 / 0
except ZeroDivisionError as e:
    pass

finally:
    raise Exception("xxxx")
"""
Traceback (most recent call last):
  File "C:/Users/satori/Desktop/satori/task.py", line 10, in <module>
    raise Exception("xxxx")
Exception: xxxx
"""

We can only see the emergence of an exception caused by our own, so we can set up a traceback

# -*- coding:utf-8 -*-
# @Author: WanMingZhu
# @Date: 2019/10/22 10:31
try:
    1 / 0
except ZeroDivisionError as e:
    import sys
    tb = sys.exc_info()[2]
finally:
    raise Exception("xxxx").with_traceback(tb)
"""
Traceback (most recent call last):
  File "C:/Users/satori/Desktop/satori/task.py", line 10, in <module>
    raise Exception("xxxx").with_traceback(tb)
  File "C:/Users/satori/Desktop/satori/task.py", line 5, in <module>
    1 / 0
Exception: xxxx
"""

In this way, it will automatically help us navigate to the statement appears abnormal. Here is caused by abnormal trackback trackback abnormal statement

We have been associated with abnormal, then the abnormal association prohibit Can it? It is clearly possible.

# -*- coding:utf-8 -*-
# @Author: WanMingZhu
# @Date: 2019/10/22 10:31
try:
    1 / 0
except ZeroDivisionError as e:
    raise Exception("xxxx") from None
"""
Traceback (most recent call last):
  File "C:/Users/satori/Desktop/satori/task.py", line 7, in <module>
    raise Exception("xxxx") from None
Exception: xxxx
"""

Show only exception of our own definition, there was no During handling of the above exception, another exception occurred:such a word.

Therefore, in the exception processing, python will set the context for the abnormality. But we can also manually by with_traceback()setting the context, or by fromspecifying an exception caused by whom. These tools are designed to get better exception traceback information printed clearly abnormal context. If the context is ignored, it can raise ···· from···Nonebe disabled automatically display abnormal context.

Guess you like

Origin www.cnblogs.com/traditional/p/11719091.html