【问题标题】:Visual Studio Code Python debugging "Exception has occurred SystemExit"Visual Studio Code Python 调试“发生异常 SystemExit”
【发布时间】:2019-02-21 16:15:39
【问题描述】:

我长期以来一直使用 Visual Studio 代码和 Python 扩展。

目前我有 Visual Studio Code 版本 1.27.2,Python 扩展“ms-python.python”版本 2018.8.0,python 版本 3.6.6,由 Windows 10 1803 企业版系统托管。

与过去相比,最新版本的 Code/MS Python 在调试过程中的表现有所不同。

过去,在 Visual Studio Code 中执行以下 Python 代码不会产生任何异常:

# Import exit from sys
from sys import exit as sys_exit
# Exit to system with errorlevel set to 1
sys_exit(1)
# Next instruction is never executed.
# Executing the script directly from the terminal in Windows sets the
# errorlevel to 1 and the script execution is terminated. 
print('unreachable code')

现在执行 sys_exit(1) 指令时结果是 SystemExit 异常 (VSCode screenshot)。

问题:有没有办法禁用此行为并返回上一个行为?

【问题讨论】:

  • 我查看了 VS Code 源代码,这对他们来说应该很容易实现。他们在 Visual Studio Code 中内置了此功能,您可以将调试器配置为忽略特定的异常类型。
  • 我在这里向 VS Code 项目提出了功能请求:github.com/microsoft/vscode/issues/91822。请看一下。
  • 注意:理论上,'unreachable code' print 语句应该是不可访问的,因为根据定义, sys.exit() 应该是代码的硬退出,并且不应运行更多行。跨度>
  • 这是真正的问题。请点赞。它仍然开放:github.com/microsoft/vscode-python/issues/850

标签: python-3.x visual-studio-code


【解决方案1】:

我在调试烧瓶应用程序时遇到了类似的情况。我的解决方法是在调试窗口底部的断点菜单中取消选中“未捕获的异常”。

【讨论】:

  • 对于某些人来说这可能是一种解决方法,但是如果您要调试某些测试,则会出现问题:取消选中“未捕获的异常”会导致没有测试运行。此行为被视为错误,因此将被修复:github.com/Microsoft/ptvsd/issues/982#issuecomment-435584288
  • 截至 2019 年 10 月仍未修复!
  • 截至 2020 年 5 月仍未修复。
  • sys.exit() 是您退出 Python 脚本的方式,不是吗!?不明白为什么它会是一个例外。 :(
  • 2021 在这里,仍然没有修复。 sys.exit(1) 产生:发生异常:SystemExit
【解决方案2】:

sys.exit 可以有参数。如果你不给它一个,它会返回 None。如果返回 0 以外的任何内容,则将其视为错误退出。所以使用这个作品

sys.exit(0)

【讨论】:

  • 控制台应用程序返回非 0 退出代码不是异常,也不一定是错误。
  • 更改应用程序的行为以解决 IDE 限制并不是一个好的解决方案。
猜你喜欢
  • 2017-09-20
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多