Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

raise

Python’s raise statement explicitly raises an exception, re-raises the active exception, or chains a new exception to an existing cause.

How it works

The raise statement explicitly signals an exception, can re-raise the active exception inside an exception handler, and optionally chains another exception with from. Its operand may be an exception class or instance derived from BaseException.

Syntax

raise_stmt: "raise" [expression ["from" expression]]

Parameters

NameTypeDescription
expressionexception class or instanceException class or instance to raise; it must be a subclass or instance of BaseException.
from expressionexception class or instanceOptional exception class or instance used as the explicit cause.

Returns

Does not return normally; it raises an exception or re-raises the active exception.

Key points

  • The syntax is raise, optionally followed by an exception expression and an optional from expression.
  • A bare statement re-raises the active exception.
  • With an expression, Python evaluates it as the exception to raise.
  • Supplying an exception class is shorthand for creating it with no constructor arguments.
  • Raised exceptions normally receive an automatically created traceback.
  • The optional from clause records a causal relationship between exceptions.

Common mistakes

  • A bare raise is valid only while an exception is actively being handled; otherwise it raises RuntimeError.
  • The expression after raise must be an exception class or instance, not an arbitrary value.
  • The exception after from becomes the explicit cause when it is an instance.

Example: Validate input and chain the cause

def parse_port(value):
    try:
        port = int(value)
    except ValueError as exc:
        raise ValueError("port must be an integer") from exc
    if not 1 <= port <= 65535:
        raise ValueError("port out of range")
    return port

try:
    parse_port("http")
except ValueError as exc:
    print(exc)

The function validates a port, raises a clearer exception, and preserves the original conversion error through chaining.

When to use it

Reject invalid arguments

Input validation

Expose a domain-specific exception while retaining the cause

Error translation

Log or inspect an error, then re-raise it

Exception handling

Raise an exception with a selected traceback

Traceback control

Interview questions

QWhat forms can the expression after raise take?

raise SomeError raises an exception class, which Python instantiates without arguments when needed. raise SomeError("detail") raises an already-created exception instance; both must derive from BaseException.

Follow-up: What does bare raise do? Inside an exception handler it re-raises the active exception; outside one, it produces RuntimeError.

QWhat does a bare raise mean?

Bare raise re-raises the exception currently being handled, preserving the active exception. It is commonly used in an except block after logging or cleanup.

Follow-up: How can a handler add context while retaining the original failure? Use raise NewError(...) from original, which establishes explicit exception chaining.

QWhat is the purpose of raise ... from ...?

The optional from expression enables exception chaining. The second expression must be an exception class or instance, and an instance is recorded as the raised exception’s __cause__.

Follow-up: How can an exception be given a particular traceback? Call with_traceback() on the exception and raise the returned same instance.

  • try
  • except
  • BaseException
  • Exception
  • with_traceback()
  • __cause__
  • __traceback__

Source: 7. Simple statements. Checked 2026-10-16.