-1

I want to interrupt my while True loop in below code but it does not work:

try:  
    while True:  
        print(1)    
except KeyboardInterrupt:  
    print('interrupted!')  
    exit()

I am running this code in PyCharm. Why isn't Ctrl+C triggering KeyboardInterrupt?

2 Answers2

2

Have you tried pressing Ctrl+C, because that's the correct way of causing KeyboardInterrupt

As @Chris mentioned it won't work for pycharm. Try using Ctrl+F2 for that

ReKx
  • 906
  • 1
  • 10
  • 23
2

In PyCharm Ctrl+C is bound to "copy". Try Ctrl+F2 instead.

Chris
  • 112,704
  • 77
  • 249
  • 231
  • The documentation this links to appears to be up to date, @Chiel. What OS are you on? – Chris Jun 06 '19 at 01:46
  • I am currently on Windows 10. Ctrl + F2 does terminate the program, but it does not trigger a KeyboardInterrupt exception. – Chiel Jun 06 '19 at 16:39