4

I'm having a hard time handling Exceptions in pycharm 3.8:

When I press ctrl+c running my program, it doesn't work, so I've been told to use pycharm console to test it, and it does work, interrupting the keyboard input.

def readFloat(msg):
    while True:
        try:
            return float(input(f'{msg}'))
        except (ValueError, TypeError):
            print(f'\033[31mError. Not valid.\033[m')
            continue
        except KeyboardInterrupt:
            print('\033[31mYou didn\'t type a number.\033[m')
            return 0


b = readFloat('Your Number: ')
print(f'\nThat\'s your number: {b}')

But now, when I try to Control+C, it doesn't catch my except and print my custom error report, returning 0. It gives me some huge and red error lines:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/User/PycharmProjects/Curso/Aula 23/ex113.py", line 35, in <module>
    b = readFloat('Your Number: ')
  File "C:/Users/User/PycharmProjects/Curso/Aula 23/ex113.py", line 26, in readFloat
    return float(input(f'{msg}'))
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_stdin.py", line 64, in readline
    requested_input = self.rpc_client.requestInput()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_transport.py", line 226, in _req
    return super(TSyncClient, self)._req(_api, *args, **kwargs)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 160, in _req
    return self._recv(_api)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 192, in _recv
    raise v
console_thrift.KeyboardInterruptException: KeyboardInterruptException()

So I tried to add a generic exception just to print the error class, and I got this:

Error found: <class 'console_thrift.KeyboardInterruptException'>

So I can't manage to detect an except keyboardInterrupt and work with it, just using a generic exception, any ideas?

Edit

The only plugin that I added was a Theme UI to run pycharm totally black, the rest of them came with the instalation, I think. I ran the .py file using CMD and it works just fine, detecting the keyboard interrupt.

Victor Lompa
  • 41
  • 1
  • 4

2 Answers2

0

Try this:

try:
    from console_thrift import KeyboardInterruptException as KeyboardInterrupt
except ImportError:
    pass
Friedrich S
  • 86
  • 1
  • 4
-1

Ref: Why doesn't this python keyboard interrupt work? (in pycharm)

The thing about IDEs is that they are not quite the same as running normally, especially when it comes to handling of keyboard characters. The way you press ctrl-c, your IDE thinks you want to copy text. The python program never sees the character. Perhaps it brings up a separate window when running? Then you would select that window before ctrl-c.

  • Sorry, I thought giving reference is enough. Also I am not aware of how to mark an answer as duplicate.I will check on that from here on. Thanks. – Karthik Radhakrishnan Aug 01 '20 at 19:03
  • Providing a link does avoid plagiarism, ya, but it's better, and is site policy, to point people to existing answers instead of copying-and-pasting the same data all over the place. Under the question, you can click `close`->`duplicate`->Then provide the link that you believe answers the question. – Carcigenicate Aug 01 '20 at 19:04
  • is there any minimum Reputation points before you get these options? – Karthik Radhakrishnan Aug 01 '20 at 19:25
  • I thought it was 500, but it may be higher. In a case like that then, until you have the ability, you can post a link in the question suggesting the dupe, and someone with the ability can close it on your behalf. – Carcigenicate Aug 01 '20 at 19:26
  • Close voting requires 3000 reputation, but you can flag as a duplicate once you have 50 reputation. – Craig Aug 01 '20 at 20:52