Chapters
Python114 chapters

ReferenceKeywords

raise

Signals a problem, stopping the function and travelling up until something catches it.

Signature

raise Exception(message)

Returns

Nothing.

Example

Python
def set_age(age):
    if age < 0:
        raise ValueError("age cannot be negative")
    return age

try:
    set_age(-1)
except ValueError as problem:
    print("ValueError:", problem)

Output

ValueError: age cannot be negative

See also