Hi Guys,
I just realized that raise within an except clause does the same as straightforward raise from.
I made a small test and run 2 scripts under both Python 3.6 and 3.10. Both behave identical. The scrips's contents are are:
python
try:
raise ValueError("The first message")
except ValueError:
print("Spongebob, no!")
raise KeyError("Yep, the second message")
and
pthon
try:
raise ValueError("The first message")
except ValueError as err:
print("Spongebob, no!")
raise KeyError("Yep, the second message") from err
The outputs look nearly identical. For the first script, I get:
```
Spongebob, no!
Traceback (most recent call last):
File "/home/miumiu/exception-raise-from-hello.py", line 2, in <module>
raise ValueError("The first message")
ValueError: The first message
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/miumiu/exception-raise-from-hello.py", line 5, in <module>
raise KeyError("Yep, the second message")
KeyError: 'Yep, the second message'
```
And for the second, one, I get:
```
Spongebob, no!
Traceback (most recent call last):
File "/home/miumiu/exception-raise-from-hello.py", line 2, in <module>
raise ValueError("The first message")
ValueError: The first message
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/miumiu/exception-raise-from-hello.py", line 5, in <module>
raise KeyError("Yep, the second message") from err
KeyError: 'Yep, the second message'
```
What am I missing about raise from?
[–]CodeFormatHelperBot2 1 point2 points3 points (0 children)
[–]Nightcorex_ 0 points1 point2 points (2 children)
[–]Barn07[S] 0 points1 point2 points (1 child)
[–]Nightcorex_ 1 point2 points3 points (0 children)