1

In my Python script, I operate an infinite loop. I want to make sure that if I stop it, all the changes I made are reversed.

Currently, I execute the script directly from the terminal, and I stop the script via STRG+C.

a = 0

try:
    while True:
        a = 1
except KeyboardInterrupt:
    a = 0

However, I want to execute the script in PyCharm. There, I can't use STRG+C, but the "Stop" button, which will result in PyCharm returning Process finished with exit code -1. How can I catch this exception?

cheesus
  • 1,111
  • 1
  • 16
  • 44

0 Answers0