🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

raise Keyword

AI & DATA SCIENCE // raise-keyword

The raise statement triggers an exception explicitly, either creating a new one or re-raising one that's already being handled.

Syntax

raise ValueError("message")
raise                    # re-raise the current exception
raise NewError(...) from original_error

Deep Dive Course

Writing raise followed by an exception type and message creates and immediately throws an exception, interrupting normal execution and searching up the call stack for a matching except clause. Inside an except block, a bare raise with no arguments re-raises the exception currently being handled, preserving its original traceback — useful when you want to log or react to an error but still let it propagate. Chaining with `from` explicitly links a new exception to the one that caused it, which shows up clearly in the traceback as the original being the direct cause of the new one.

1Understanding raise Keyword

Writing raise followed by an exception type and message creates and immediately throws an exception, interrupting normal execution and searching up the call stack for a matching except clause. Inside an except block, a bare raise with no arguments re-raises the exception currently being handled, preserving its original traceback — useful when you want to log or react to an error but still let it propagate. Chaining with from explicitly links a new exception to the one that caused it, which shows up clearly in the traceback as the original being the direct cause of the new one.

💡

Use a bare raise, with no arguments, to re-raise the currently-handled exception unchanged, rather than raising the caught exception object again explicitly, which subtly resets part of the traceback information.

editor.html
def set_age(age):
    if age < 0:
        raise ValueError("Age cannot be negative")
    return age

try:
    set_age(-5)
except ValueError as e:
    print(e)
localhost:3000

2Practical Example

Here is a real-world application of raise Keyword showing how it is used in production Python code.

editor.html
def load_config():
    try:
        return int("not-a-number")
    except ValueError as e:
        raise RuntimeError("Failed to load config") from e

try:
    load_config()
except RuntimeError as e:
    print(e)
    print(type(e.__cause__))
localhost:3000

3Best Practices

Follow these guidelines when working with raise Keyword:

1. Raise specific, meaningful built-in exception types (or your own custom ones) instead of a generic Exception, so callers can catch precisely what they expect

2. Use a bare raise inside except to re-propagate an exception after logging or partial handling, instead of raising the captured exception object again

3. Use raise NewError(...) from original_error when wrapping a low-level exception in a higher-level one, so the original cause remains visible in the traceback

⚠️

Tip: Use a bare raise, with no arguments, to re-raise the currently-handled exception unchanged, rather than raising the caught exception object again explicitly, which subtly resets part of the traceback information.

editor.html
def set_age(age):
    if age < 0:
        raise ValueError("Age cannot be negative")
    return age

try:
    set_age(-5)
except ValueError as e:
    print(e)
localhost:3000

Examples

Example 01Basic Usage
def set_age(age):
    if age < 0:
        raise ValueError("Age cannot be negative")
    return age

try:
    set_age(-5)
except ValueError as e:
    print(e)
Example 02Advanced Example
def load_config():
    try:
        return int("not-a-number")
    except ValueError as e:
        raise RuntimeError("Failed to load config") from e

try:
    load_config()
except RuntimeError as e:
    print(e)
    print(type(e.__cause__))

Best Practices

  • Raise specific, meaningful built-in exception types (or your own custom ones) instead of a generic Exception, so callers can catch precisely what they expect
  • Use a bare raise inside except to re-propagate an exception after logging or partial handling, instead of raising the captured exception object again
  • Use raise NewError(...) from original_error when wrapping a low-level exception in a higher-level one, so the original cause remains visible in the traceback

Interview Question

What's the difference between writing a bare raise and re-raising the captured exception object explicitly inside an except block?

Hint: Both seem to re-raise the same exception, but one subtly changes its traceback.

A bare raise re-raises the currently active exception exactly as it was, preserving its full original traceback, including the frames from where it was first raised. Explicitly raising the captured exception object instead treats it as if it were a brand-new exception being raised right at that line, which resets the traceback to start from that point, losing some of the original context about where the error actually first occurred deeper in the call stack.

Exercises

MediumPractice using raise Keyword in a real scenario.
View Solution
def set_age(age):
    if age < 0:
        raise ValueError("Age cannot be negative")
    return age

try:
    set_age(-5)
except ValueError as e:
    print(e)

Frequently Asked Questions

What's the difference between writing a bare raise and re-raising the captured exception object explicitly inside an except block?

A bare raise re-raises the currently active exception exactly as it was, preserving its full original traceback, including the frames from where it was first raised. Explicitly raising the captured exception object instead treats it as if it were a brand-new exception being raised right at that line, which resets the traceback to start from that point, losing some of the original context about where the error actually first occurred deeper in the call stack.

Related Functions

custom-exceptionstry-blockexcept-block