12

Why doesn't code like the following catch CTRL-C?

MAXVAL = 10000
STEP_INTERVAL = 10

for i in range(1, MAXVAL, STEP_INTERVAL):
    try:
        print str(i)
    except KeyboardInterrupt:
        break

print "done"

My expectation is -- if CTRL-C is pressed while program is running, KeyboardInterrupt is supposed to leave the loop. It does not.

Any help on what I'm doing wrong?

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
user63503
  • 6,243
  • 14
  • 41
  • 44
  • It works for me on Windows, Python 2.5 – jfs Feb 12 '09 at 21:57
  • 2
    you didn't tell us what happens instead! is the interrupt ignored or does it stop the program without printing the "done"? –  Feb 13 '09 at 03:00

3 Answers3

21

Sounds like the program is done by the time control-c has been hit, but your operating system hasn't finished showing you all the output. .

17

code flow is as follows:

  1. for grabs new object from list (generated by range) and sets i to it
  2. try
  3. print
  4. go back to 1

If you hit CTRL-C in the part 1 it is outside the try/except, so it won't catch the exception.

Try this instead:

MaxVal = 10000
StepInterval = 10

try:
    for i in range(1, MaxVal, StepInterval):
        print i
except KeyboardInterrupt:
    pass

print "done"
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • I believe that print("done") should be done inside the except block, or your code does not seem to follow very good practices imo. Exceptions should never _just_ pass –  Sep 11 '18 at 14:06
  • 2
    @J.C.Rocamonde in this specific code, OP wants to ignore the CTRL-C, and print "done" in all situations. If I put the `print` statement inside the `except` clause, then it will be printed only when pressing ctrl-c and not when the for-loop finishes normally. Exceptions can be just `pass` when they are expected, and you want to just ignore then, and do nothing about it. It is a valid approach, as long as you know what you're ignoring. I would say bare `except` without specifing a exception class is bad, but `pass` is fine. – nosklo Sep 11 '18 at 16:11
  • 1
    Yeah you are right; the print should be regardless. I was under the wrong impression (don't know why) that the loop was infinite. Sorry for complaining :) –  Sep 11 '18 at 16:15
13

I was having this same problem and I just found out what was the solution:

You're running this code in an IDE like PyCharm. The IDE is taking ctrl+c (keyboardinterrupt) as copy. Try running your code in the terminal.

  • And you say that because? Very good attempt to just guess he is using an IDE. It could be run in the interactive console, or with the terminal... –  Sep 11 '18 at 14:07
  • 1
    Or the issue could depend on the IDE if that is really the problem, –  Sep 11 '18 at 16:16
  • I know this post is somewhat old but I upped this because I ran into this exact problem and this solution helped. Terminal fixed it. – Aspen Jun 27 '19 at 04:22
  • This issue can still arise in 2022. See this other thread for a fix: https://stackoverflow.com/questions/39796689/why-doesnt-this-python-keyboard-interrupt-work-in-pycharm – basil_man May 28 '22 at 15:05
  • Thanks for the answer. It's Ctrl + F2 in Pycharm – Ovi Oprea Aug 25 '23 at 10:54