Python Keyboardinterrupt

As we are very well aware of what exceptions are and how to manage them in Python, we can go on to the next section. In layman’s terms, exceptions are anything that disrupts the program’s normal flow. Similarly, KeyboardInterrupt is a Python exception that is thrown when a user or programmer interrupts a program’s usual execution.

While executing the program, the Python interpreter checks for any interrupts on a regular basis. When a user or programmer mistakenly or purposely pushes the ctrl – c or del key in Python, the interpreter throws the KeyboardInterrupt exception.

The KeyboardInterrupt exception inherits the BaseException and, like other Python exceptions, is handled via a try-except statement to prevent the interpreter from abruptly quitting the program.

KeyboardInterrupt Exception and How Does it Work?

The KeyboardInterrupt exception is a standard exception that is thrown to manage faults with the keyboard. In Python, there is no special syntax for the KeyboardInterrupt exception; it is handled in the usual try and except block. The code that potentially causes the problem is written inside the try block, and the ‘raise’ keyword is used to raise the exception, or the python interpreter raises it automatically.

One of the most vexing aspects of working with Python is that it exits the program when the user presses ctrl – c, either intentionally or inadvertently, which is a major issue when dealing with large amounts of data, such as retrieving records from a database, handling, executing a large program that performs multiple tasks at once, and so on.

This exception behaves in the same way as other Python exceptions. The only difference with this exception is that the user-created it and that the computer had no part in it. Refer to the following sections to learn about this concept in detail.

Example 1:

When it comes to managing exceptions in Python, the try…except statement is employed. The try…except statement has a distinct syntax, separated into three parts, each with its own purpose and function in Python code.

The try block contains a collection of code that the interpreter must verify for errors. The except block adds the necessary exceptions to avoid the code’s faults. The final block includes the sentences that must be performed without being checked, but which the try and except blocks disregard.

We will create a small program that asks for input from the user while manually handling the KeyboardInterrupt exception to demonstrate the Python code for KeyboardInterrupt. The try…except statement is used in the following Python code to capture the KeyboardInterrupt error.

The output is shown below.

The input function is located between the try blocks in the code above and is left empty because more information is not required in this scenario. The unless block then takes care of the KeyboardInterrupt error.

To detect when the KeyboardInterrupt procedure occurs, we manually issue the KeyboardInterrupt error. Python allows the user to define as many unless blocks as they wish in a portion of code.

Example 2:

We’ll now use Signal Handlers. In Python, the signal module is used to offer functions and processes that use signal handlers.

When this happens, the KeyboardInterrupt is raised by default. The sys module in Python provides a number of useful variables and functions for managing the Python runtime environment.

The signal and sys modules must be included in the Python code to utilize this approach without errors. Signal handlers are used in the following Python code to catch the KeyboardInterrupt fault.

The outcome of the code above is as follows.

The signal.signal() function is used in the code above to specify custom handlers that will be run when a signal of a specific type is received. It is worth noting that once a handler is configured for a specific signal, it stays in place unless the user actively resets it. The handler for SIGCHLD is the only exception in this situation.

Example 3:

Here’s the last program we will look at. The code inside the try block potentially throws an exception and then takes the user’s input ‘name’. The different exception classes are then written to catch/handle the exception. If the exception classes (as seen in the code below) do not match, the rest of the code will be run.

When the user pushes the ctrl -c key, the following output appears when the software asks for the username. The print statement created for the KeyboardInterrupt exception is printed in the output when the user presses ctrl – c, which is a user interrupt exception.

When the user pushes the ctrl – d key and asks for the username, the output seen below is created. A print statement defined inside the EOF exception class is displayed when the user presses the ctrl – d button, signifying the file’s end. This specifies that if an exception is caught in the code, the appropriate exception class is searched, and the following block is run.


The KeyboardInterrupt exception, how it is raised, and how it is handled in Python are all covered in the preceding article. The KeyboardInterrupt exception, as its name suggests, is a simple exception that is raised when a program is interrupted by the user’s keyboard. It is critical for any programmer, new or experienced, to grasp every form of exception to deal with them appropriately and create a program quickly (able to handle any kind of such situation).

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content