Python

Python KeyboardInterrupt

Python “KeyboardInterrupt” is a built-in exception in python which essentially means the keyboard key combination of “CTRL + C” or “CTRL + Z”. Now, exception handling is one of the core roles of a developer, and it is also a testament to his skill. In a running program, multiple interrupts and exceptions can occur, and every programming language has special exception-handling techniques.

This article will discuss the KeyboardInterrupt exception in the Python Programming Language. The content of this article includes:

What is Python KeyboardInterrupt?

As mentioned above, the KeyboardInterrupt is a built-in or pre-defined exception in the python programming language, which means the key combination of “CTRL+C”. If you are wondering what is an exception, then it is something that can alter the default execution of the program resulting in an unexpected result.

The combination “CTRL + C” is often used to stop the processing of a particular program or a particular task. Usually, if the user presses “CTRL + C”, it causes a program to close abruptly, resulting in an error. To demonstrate this, simply type an input statement::

name = input("Enter your name")

Execute the program, and instead of inputting the name, press “CTRL + C”:

The image shows the abrupt closing of the program and the reason is mentioned as “KeyboardInterrupt”.

How to Handle KeyboardInterrupt Exception?

The KeyboardInterrupt exception is handled the same way as any other exception in the Python programming language and that is with the help of the “try-except” statements. If you are unaware of the syntax of the try-except statements, then go over the following try-except syntax:

Syntax

try:
    #Statements to try

except nameOfException:
    #Statements to execute upon that exception
   
else:
    #Statements when no exception occurs

Now that you are aware of the syntax of handling KeyboardInterrupt exceptions with the try-except statements, let’s try an example.

Example: Handling KeyboardInterrupt Through try-except

Take a simple input inside the “try” statement and then for the except statement, define the exception as “KeyboardInterrupt” using the following lines of code:

try:
    #Statements to try
    name = input("Enter your name: ")

except KeyboardInterrupt:
    #Statements to execute upon that exception
    print("You typed CTRL + C, which is the keyboard interrupt exception")    
else:
    #Statements when no exception occurs
    print("You name is ",name)

Running the following code will ask the user about his\her name, and if the user raises the Keyboard Interrupt then the program will display the following output on the terminal:

In this way, you can stop the program or the application from abruptly closing and handle the exception.

How to Cause the KeyboardInterrupt Programmatically?

In certain situations, the developer can write code that will cause an exception to occur intentionally. This practice is actually quite used, for example closing the program when the user wants to exit and more. If you are looking for a method to raise this exception then in Python, the keyword, “raise” is used to cause exceptions manually.

Syntax of raise

The syntax of raise is quite simple, it is as:

raise NameOFException

Example: Raising KeyboardInterrupt Programmatically

To raise the KeyboardInterrupt exception use the following lines of code:

try:
    raise KeyboardInterrupt

except KeyboardInterrupt:
    print("Keyboard interrupt exception Caught")    

else:
    print("No Exception")

Running the above code will produce the following output on the terminal:

As you can see, the program was able to detect a KeyboardInterrupt exception even without requiring the user to type the key combination CTRL + C.

Conclusion

A Keyboard Interrupt is nothing but a combination of keyboard input that can change or alter the normal execution flow of the program. In this python, the term “KeyboardInterrupt” is the same for the keyboard combination of “CTRL + C”, which is used to stop the execution of the program. This post has explained how to handle the KeyboardInterrupt and also how to raise it programmatically.

About the author

Admin

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.