raise Raise exception
Last updated: 2026-09-22
raise Raise exception
raise explicitly raises an exception (with optional type/message, or re-raises the current one).
| Category | Keywords & Statements |
|---|---|
| Kind | Keyword / Statement |
| Python Version | all |
📝 Syntax
raise [ExceptionType [from other]]
raise
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
caught: must be >= 0
🏷️ Related Keywords / Statements
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is the difference between `raise` and `assert`?
A`raise` unconditionally raises an exception, used to validate input and report business errors. `assert` only raises `AssertionError` when the condition is false, and it is entirely disabled under `python -O`, so it is unsuitable for formal validation.
QWhat does a bare `raise` inside an `except` block mean?
AA bare `raise` re-raises the exception currently being handled, preserving the original traceback. It is often used after logging or doing cleanup, to keep propagating the exception outward.
QWhat is the difference between `raise ValueError('msg')` and `raise ValueError`?
AThe first carries an error message, so callers see the reason via `str(e)` and debugging is easier; the second has no message. It is recommended to always provide a meaningful message.