all Python 3Exceptionsexception
KeyboardInterrupt
built-in exceptions
The user interrupted execution (Ctrl+C). Inherits BaseException, so `except Exception` won't catch it.
Syntax
raise KeyboardInterrupt
The classic trap is a bare except: or except BaseException inside a loop — it swallows Ctrl+C and the program stops being interruptible. If you do need to catch it (close a file, save progress), clean up and re-raise. The interrupt lands in the main thread and can arrive between any two instructions, so whatever you were doing may be half-finished.
Examples
try:
raise KeyboardInterrupt
except KeyboardInterrupt as e:
print(type(e).__name__) # → KeyboardInterrupt
See also
Open in the full glossarydocs.python.orgReport an inaccuracy