ConclusionĪbove article clearly explains what the KeyboardInterrupt exception is, how it is raised and is handled in Python. This indicates that the desired exception class is searched when the exception arises if catched in the code, the following block is executed. In the above output, a print statement written under the EOF exception class is displayed as the user presses the ctrl – d button which indicates the end of file. General output when the user presses the ctrl – c button. Some add a flag or variable which gets incremented when the user clicks on the ctrl – c button or some programmers creates a separate function which takes some extra input or keeps track of the user pressing ctrl- c keys and responds accordingly.It totally depends on the programmer how he/she codes in order to avoid this situation as every programmer has a different way of thinking and hence coding.So if the exception is raised and we are tangled in some infinite loop then we can write a clean code in the finally block (which will get executed in every case) which can help us to backtrack the situation. As we all know that finally block is always executed.There are few things that we can do in order to avoid this. There is no such way to avoid the KeyboardInterrupt exception in Python as it will automatically raise the KeyboardInterrupt exception when the user presses the ctrl – c.How to Avoid KeyboardInterrupt Exceptions in Python? If not found, then it will exit the program with the normal python message. If the exception is raised but does not match with the class name of the exception handler present after the except keyword, it will start looking for the respective catch block to handle it outside the inner try block. If no exceptions arise in the try block, then the execution continues in a normal manner but no ‘except’ block statements are executed.If the user presses the ctrl – c, an exception will be raised, execution of the rest of the statements of the try block is stopped and will move the except block of the raised exception.First the code inside the try block is executed.Print('Hello user there is some format error') # If both the above exception class does not match, else part will get executed Print('Hello user you have pressed ctrl-c button.') Print('Hello user it is EOF exception, please enter something and run me again') # writing the different exception class to catch/ handle the exception Name = input('Enter the name of the user ') # code inside the try block which can cause an exception In order to understand the working of KeyboardInterrupt exception in Python, lets understand the below written code first. Only thing about this exception is that it is user generated and there is no involvement of the computer in raising it. This exception works in a very simple manner like other exceptions in Python. One of the most annoying things while working with python is that it exits the program once the user presses ctrl – c either intentionally or mistakenly which is a big problem when the bulk data is getting processed like retrieving records from database, handling, executing a big program handling many tasks at a time, etc. # code to perform any specific tasks to catch that exception How does KeyboardInterrupt Exception work in Python? # raising this exception is not mandatory To catch the exception and perform desired tasks, special code inside the except block is written. Code which can cause the error is placed inside the try block with the ‘raise’ keyword to raise that exception or the python interpreter automatically raises it. There is no such specific syntax of KeyboardInterrupt exception in Python, it is handled in the normal try and except block inside the code. Hopefully she then notices highlighted tabs and/or pylint warning in the "Assistant" view and uses "Replace tabs with spaces".As seen above, KeyboardInterrupt exception is a normal exception which is thrown to handle the keyboard related issues. If the user has 4-space indents and she pastes an example with tabs, then she'll have both tabs and spaces in her file. No automatic conversion or unification of tabs and spaces is attempted anymore. If you prefer tabs, then you'll probably want to switch on "Indent with tab characters" and switch off "Highlight tab characters". Leading tab characters are displayed with same width as 4 spaces. Command "Edit => Replace tabs with spaces" (replaces each tab in the beginning of the line with 4 spaces, Add command to replace leading tabs with spaces #1411).Setting "Tools => Options => Editor => Highlight tab characters" (gives different background for tabs on by default, Allow highlighting tabs in the editor #1409).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |