python - 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
解决方案
推荐阅读
- javascript - 打字稿 | 关于缺少函数返回类型 ESLint 的警告
- apache-spark - 为什么没有 collect() 对累加器的操作不起作用?
- javascript - Knockout.Js 中的多个 afterAdd 调用
- javascript - 如何从从 nodejs 服务器获取的缓冲区数据中显示视频
- git - 仅将 TFS 存储库的一部分迁移到 Azure DevOps 上的 Git
- python - 当并非所有行都包含结束日期时,对数据框中的开始日期和结束日期进行数学运算
- java - 在 Java 中使用 SecureRandom
- amazon-web-services - 通过 CLI 删除 S3 存储桶中的空文件夹
- firebase - 服务器端迁移到 FCM,但客户端将在 2019 年 4 月 11 日之后将 GCM 与 InstanceID.getToken() 一起使用
- excel - Excel 查找和替换无法按预期工作