raise statements

https://docs.python.org/3/reference/simple_stmts.html#raise

INTRODUCTION

The 'raise' statement is used to explicitly raise an exception. A traceback object is normally created automatically when an exception is raised and attached to it as the '__traceback__' attribute, which is writable.


# Syntax.
#
# raise exception_instance
# raise exception_class
#
# Using the context of the old exception (Py3)
#
# raise new_exception from old_exception
# raise new_exception from None   # suppress exception chaining

raise IndexError()
raise IndexError("message")   # recommended

raise IndexError
raise        # re-raise the last exception that was active in the current scope

EXAMPLES


def power3(x):
    if x < 0:
        raise ValueError("negative x")
    return x * x * x