Skip to main content

How to Re-raise an Exception in Python with raise

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Sometimes a piece of code needs to react to an error, such as logging it or undoing a change, without being the place that decides what happens next. A bare raise inside except sends the same exception on, with its original traceback, after your code has run. This guide shows it with a small number parser, with real output from Python 3.15.2.

Syntax​

Syntax
try:
<code that may fail>
except <ExceptionType>:
<what to do first>
raise
PartWhat it means
except <ExceptionType>:Catches the error so your code runs first
raise with nothing after itRaises the same exception again, unchanged

Python's language reference: "If no expressions are present, raise re-raises the exception that is currently being handled, which is also known as the active exception. If there isn't currently an active exception, a RuntimeError exception is raised indicating that this is an error."

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

read_age.py
def read_age(text):
try:
return int(text)
except ValueError:
print(f"Logging: can't read {text} as a number")
raise


print(read_age("42"))
print(read_age("forty"))

The read_age.py script in a macOS-style code window: read_age converts text with int, and on ValueError prints a log line and re-raises with a bare raise; it's called with 42 and with forty.

Step 2: Run It​

Terminal
uv run python read_age.py
Output
42
Logging: can't read forty as a number
Traceback (most recent call last):
File "/Users/you/source/python-basics/read_age.py", line 10, in <module>
print(read_age("forty"))
~~~~~~~~^^^^^^^^^
File "/Users/you/source/python-basics/read_age.py", line 3, in read_age
return int(text)
ValueError: invalid literal for int() with base 10: 'forty'

The output in a terminal: 42, the log line about forty, then a traceback pointing at return int(text) on line 3 and ending in ValueError: invalid literal for int() with base 10: 'forty'.

The log line printed first, then the original ValueError carried on, and its traceback still points at return int(text) on line 3, where it began. The caller decides what to do with it; here nobody catches it, so the script stops.

Common Mistakes​

MistakeWhat happensDo this instead
Using a bare raise outside an except blockRuntimeError: No active exception to reraiseUse it only while an exception is being handled
Catching an error, logging it and not re-raisingThe caller never learns that something failedRe-raise unless your code truly handled it
Raising a new exception that hides the originalThe real cause disappears from the tracebackUse a bare raise, or raise NewError(...) from error

Troubleshooting​

SymptomLikely causeFix
The same error is logged twiceTwo layers each log, then re-raiseLog in one place only, usually the outermost

FAQ​

What's the difference between raise and raise error?​

After except ValueError as error:, both raise the same exception object. The bare form needs no name and says plainly that the exception continues unchanged.

Can I add information before re-raising?​

Yes. Since Python 3.11, error.add_note("...") adds a line to the traceback before you raise.

Conclusion​

Inside except ValueError, the parser printed its log line and the bare raise sent the original ValueError on, with its traceback still pointing at int(text).

Additional Resources​