5

I have my own custom sys.excepthook to handle errors (via a custom error message) and it runs fine when run directly. Since updating PyCharm to version 2019.3 this function it does not work when debugging. Used to work in older versions. Instead, PyCharm just prints the stack and terminates the process.

Here's a basic code. Works when run directly but fails when debugged.

import sys
def myExceptHook(type, value, tb):
    print("there's been an error")

# reroute every exception through my hook
sys.excepthook = myExceptHook
raise Exception('error')

When debugging I get the following output:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1434, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Projects/TubeEye/code/testraise.py", line 8, in <module>
    raise Exception('error')
Exception: error

But if running directly I get (as expected):

there's been an error
uri
  • 392
  • 2
  • 6
  • I don't use pycharm, but my guess is, that pycharm overwrites the `sys.excepthook` for its own purposes. – gelonida Dec 15 '19 at 13:26
  • 1
    @gelonida this was my thought too but if that was the case it would probably overwrite it BEFORE i was hijacking it. Also in previous versions this didn't occur so I was thinking maybe there was a way to disable to work around this. – uri Dec 15 '19 at 14:15
  • perhaps you could add following lines just for debugging. `print(sys.excepthook)` then `sys.excepthook = myExceptHook` then again `print(sys.excepthook)` and then `raise Exception('error')` Is your code perhaps executed in a thread? https://stackoverflow.com/questions/1643327/sys-excepthook-and-threading#31622038 – gelonida Dec 15 '19 at 19:59
  • I have the same issue. Did you file a bug in their base? – Joe Jan 25 '20 at 12:52
  • 2
    @Joe I did. This is now verified as a bug in PyCharm (regression) starting from version 2019.1. Versions 2018.x are unaffected. Link to the posted issue here in case someone wants to watch it: youtrack.jetbrains.com/issue/PY-40039 – uri Jan 25 '20 at 20:11
  • 2
    Actually this is the issue I filed: the other one is similar. https://youtrack.jetbrains.com/issue/PY-39723 – uri Jan 25 '20 at 20:15

0 Answers0