python raise ... from statement
- aliases
- No value
- tags
- No value
- description
- No value
- links
- http://legacy.python.org/dev/peps/pep-3134/ 0014 Python ๐
- status
- No value
- title
- python raise ... from statement
- created
- 2025-01-07T14:25:07
- updated
- 2025-01-07T14:32:06
TL;DR
The "raise from" statement in Python is used to raise a new exception while preserving the context of the original exception. This allows you to provide additional information about the error that occurred, making debugging easier.
try:
raise ValueError
except Exception as e:
raise IndexError from e
PEP-3134
http://legacy.python.org/dev/peps/pep-3134/
๋ค์ ์ ์์์๋ ์ธ๊ฐ์ง dunder attributes๋ค์ด ํฌํจ๋๋ค:
__context__: ์๋ฌต์ ์ผ๋ก ์ฒด์ด๋๋ ์์ธ. ์์ธ๊ฐ ์ฒ๋ฆฌ๋์ง ์๊ณ ์ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ์๋์ผ๋ก ์ค์ ๋๋ attribute__cause__: ๋ช ์์ ์ผ๋ก ์ฒด์ด๋๋ ์์ธ, ์๋ก ๋ฐ์ํ ์์ธ๊ฐ ๋ค๋ฅธ ์์ธ๋ก ์ธํด ๋ฐ์ํ์์ ๋ํ๋ธ๋ค.raise IndexError from e๊ตฌ๋ฌธ์ ์ฌ์ฉํด ์ค์ ํ ์ ์๋ค.
__traceback__: ๋ฐฑํธ๋ ์ด์ค์ ๊ด๋ จํ ์ ๋ณด (์์ธ ๋ฐ์์ ์ถ๋ ฅ๋ ์คํํ๋ ์)
์ด ์ ์ ๋๋ถ์ Python 3.0 ์ดํ๋ถํฐ ํ๋์ ์์ธ๊ฐ ๋ค๋ฅธ ์์ธ์ ์ํด ๋ฐ์ํ์์ ๋ช ์์ ์ผ๋ก ํํํ ์ ์๊ฒ๋๋ค.
์์ 1: raise ... from ...์ผ๋ก ๋ช
์์ ์์ธ ์ฒด์ธ ์ค์ (__cause__)
try:
# ์ฒซ ๋ฒ์งธ ์์ธ ๋ฐ์
raise ValueError("์๋ ์์ธ")
except ValueError as e:
# ๋ ๋ฒ์งธ ์์ธ๋ฅผ ๋ฐ์์ํค๋ฉฐ ์๋ ์์ธ๋ฅผ ๋ช
์์ ์ผ๋ก ์ฐ๊ฒฐ
raise RuntimeError("์๋ก์ด ์์ธ") from e
์ถ๋ ฅ ๊ฒฐ๊ณผ:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
ValueError: ์๋ ์์ธ
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
RuntimeError: ์๋ก์ด ์์ธ
- ์ค๋ช
:
raise RuntimeError("์๋ก์ด ์์ธ") from e๋ValueError๊ฐRuntimeError์ ์ง์ ์ ์ธ ์์ธ์์ ๋ํ๋ ๋๋ค.- ์ด๋ก ์ธํด ์ถ๋ ฅ์
The above exception was the direct cause of the following exception:๋ฉ์์ง๊ฐ ์ถ๊ฐ๋ฉ๋๋ค. - ์ด ๊ด๊ณ๋
__cause__์์ฑ์ ์ ์ฅ๋ฉ๋๋ค.
์์ 2: ์๋ฌต์ ์ฐ๊ฒฐ (__context__)
try:
# ์ฒซ ๋ฒ์งธ ์์ธ ๋ฐ์
raise KeyError("์ฒซ ๋ฒ์งธ ์์ธ")
except KeyError:
# ๋ ๋ฒ์งธ ์์ธ ๋ฐ์ (์๋ฌต์ ์ผ๋ก ์ฐ๊ฒฐ๋จ)
raise TypeError("๋ ๋ฒ์งธ ์์ธ")
์ถ๋ ฅ ๊ฒฐ๊ณผ:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
KeyError: '์ฒซ ๋ฒ์งธ ์์ธ'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
TypeError: ๋ ๋ฒ์งธ ์์ธ
- ์ค๋ช
:
TypeError๋KeyError์ฒ๋ฆฌ ์ค์ ๋ฐ์ํ์ผ๋ฏ๋ก ์๋ฌต์ ์ผ๋ก ์ฐ๊ฒฐ๋ฉ๋๋ค.- ์ด ๊ด๊ณ๋
__context__์์ฑ์ ์ ์ฅ๋๋ฉฐ, ์ถ๋ ฅ์During handling of the above exception, another exception occurred:๋ฉ์์ง๊ฐ ์ถ๊ฐ๋ฉ๋๋ค.
์์ 3: __suppress_context__ = True๋ก ์ด์ ์์ธ ์ต์
try:
# ์ฒซ ๋ฒ์งธ ์์ธ ๋ฐ์
raise IndexError("์ฒซ ๋ฒ์งธ ์์ธ")
except IndexError:
# ์ด์ ์์ธ๋ฅผ ์ต์ ํ๊ณ ์๋ก์ด ์์ธ ๋ฐ์
ex = ValueError("๋ ๋ฒ์งธ ์์ธ")
ex.__suppress_context__ = True
raise ex
์ถ๋ ฅ ๊ฒฐ๊ณผ:
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
ValueError: ๋ ๋ฒ์งธ ์์ธ
- ์ค๋ช
:
__suppress_context__ = True๋ฅผ ์ค์ ํ๋ฉด ์ด์ ์ ๋ฐ์ํIndexError๊ฐ ์ถ๋ ฅ๋์ง ์์ต๋๋ค.- ์ด๋ ๋ถํ์ํ ๋๋ฒ๊น ์ ๋ณด๋ฅผ ์ ๊ฑฐํ๊ณ ์๋ก์ด ์์ธ๋ง ํ์ํ๊ณ ์ถ์ ๋ ์ ์ฉํฉ๋๋ค.
์์ฝ
- ๋ช
์์ ์ฐ๊ฒฐ:
raise ... from ...โ__cause__ - ์๋ฌต์ ์ฐ๊ฒฐ: ์ฒ๋ฆฌ ์ค ๋ค๋ฅธ ์์ธ ๋ฐ์ โ
__context__ - ์ถ๋ ฅ ์ต์ :
__suppress_context__ = Trueโ ์ด์ ์์ธ ์จ๊น
์ด ๊ธฐ๋ฅ์ ๋๋ฒ๊น ์ ๋ฌธ์ ์ ์์ธ์ ๋ ๋ช ํํ ํ์ ํ๋ ๋ฐ ๋งค์ฐ ์ ์ฉํฉ๋๋ค! ๐