KeyboardInterrupt

Built-in ExceptionPython 2.0+Beginner

Raised when the user presses Ctrl+C

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
# Triggering and catching KeyboardInterrupt
try:
    raise KeyboardInterrupt("simulated interrupt")
except KeyboardInterrupt as e:
    print(f"Caught KeyboardInterrupt: {e}")
    print(f"Type: {type(e).__name__}")

KeyboardInterrupt is raised when raised when the user presses ctrl+c. Always catch specific exceptions rather than bare except clauses.

Try in Playground

Tags

exceptionerror-handlingcore