raise — Expert Examples
Throws an exception manually
Raise bytecode and exception internals
How raise works under the hood.
python
import dis def raise_explicit(): raise ValueError("test") def raise_bare(): try: 1/0 except: raise def raise_from(): try: 1/0 except ZeroDivisionError as e: raise RuntimeError("wrapped") from e print("raise ValueError('test'):") dis.dis(raise_explicit) print("\nbare raise:") dis.dis(raise_bare) print("\nraise ... from ...:") dis.dis(raise_from)
raise compiles to RAISE_VARARGS with 1 arg (exception), bare raise uses 0 args (re-raise), and 'raise from' uses 2 args (exception + cause).
Want to try these examples interactively?
Open Expert Playground