Built-in Exceptions

KeyboardInterrupt — exception class

Represents user interruption, commonly Ctrl+C. It can arrive at unpredictable points, so prefer finally or context managers for cleanup and allow interruption to propagate rather than treat it as an ordinary recoverable error. The example raises a controlled stand-in to demonstrate handling; it does not cause the real system failure.

Formal Syntax & Structure

python syntaxpython
KeyboardInterrupt

Return Value

type

The class object KeyboardInterrupt; this reference documents the class identity, not a universal constructor signature.

Working Example

Example Snippetpython
try:    raise KeyboardInterrupt()except KeyboardInterrupt as error:    print(type(error).__name__)
Python Reference