raise
和raise from
Python 和有什么不一样?
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
raise IndexError from None
,说。
raise IndexError from False
引发一个TypeError
,而不是一个IndexError
。让我开心。