首页 > 解决方案 > PyCharm下调试时不调用sys.excepthook

问题描述

我有自己的自定义 sys.excepthook 来处理错误(通过自定义错误消息),直接运行时运行良好。由于将 PyCharm 更新到 2019.3 版本,此功能在调试时不起作用。用于在旧版本中工作。相反,PyCharm 只是打印堆栈并终止进程。

这是一个基本代码。直接运行时有效,但调试时失败。

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')

调试时,我得到以下输出:

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

但如果直接运行,我会得到(如预期的那样):

there's been an error

标签: pythondebuggingpycharm

解决方案


推荐阅读