Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Keyword raise

Raise a BaseException instance or a suitable exception class.

Version: Python 3.14; repository image, production runtime not attested

Syntax

raise NewError(message) from original

Raise a BaseException instance or a suitable exception class.

A bare raise propagates the active exception; outside active handling it raises RuntimeError. Explicit from attaches a cause to a translated exception.

The example preserves the conversion failure while exposing a domain-specific message.

Examples

def quantity(text):
    try:
        return int(text)
    except ValueError as original:
        raise RuntimeError("invalid quantity") from original
try:
    quantity("two")
except RuntimeError as error:
    print(str(error), type(error.__cause__).__name__)
try:
    raise
except RuntimeError:
    print("no active exception")

Result

invalid quantity ValueError
no active exception

The example preserves the conversion failure while exposing a domain-specific message.

Sources