Keywords and Control Flow

raise — Signal and propagate exceptions

Signal an exception using a BaseException instance or subclass. A class is instantiated without arguments if raised directly. Bare raise propagates the active exception and fails with RuntimeError when none is active. Explicit chaining sets the cause; raising a new exception during handling also preserves implicit context. Using from None suppresses display of implicit context without removing the stored context. Use specific exceptions that describe failures and preserve useful causes when translating errors.

Formal Syntax & Structure

python syntaxpython
raise ExceptionTyperaise ExceptionType(message)raiseraise NewError(message) from original

Working Example

Example Snippetpython
def quantity(text):    try:        return int(text)    except ValueError as original:        raise ValueError("invalid quantity") from original
try:    quantity("many")except ValueError as error:    print(error)    print(type(error.__cause__).__name__)
Python Reference