【问题标题】:VSCode on Windows: gdb doesn't break on 'throw' but breaks on regular exceptionsWindows 上的 VSCode:gdb 不会因“抛出”而中断,但会因常规异常而中断
【发布时间】:2020-04-04 03:51:28
【问题描述】:

在调试我的代码时,gdb 会捕获常规异常(例如除以零),但不会捕获自定义异常,并使用 throw 抛出。

我希望 vscode 会跳转到引发异常的位置,因此我可以进行调查。相反,该异常会导致应用程序终止并关闭调试器。

我用一些虚拟代码对其进行了测试:

编译为:g++ -g main.cpp -o test.exe

#include <stdexcept>
#include <iostream>

void test()
{
    throw std::invalid_argument( "received negative value" );
}

int main(int argc, char const *argv[]) {
    std::cin.get();

    // int c = 1 / 0;
    test();

    std::cout << "v";

    std::cin.get();
    return 0;
}

输出:

terminate called after throwing an instance of 'std::invalid_argument'
  what():  received negative value

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

我的环境:

  • Windows 7 64 位
  • VSCode 1.40.2
  • MinGW

launch.json

        {
            "type": "cppdbg",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceRoot}/test.exe",
            "cwd": "${workspaceRoot}",
            "miDebuggerPath": "C:/MinGW/bin/gdb.exe",
            "MIMode": "gdb",
            "preLaunchTask": "Compile",
            "externalConsole": true
        }

【问题讨论】:

  • 有一个解决方法。我可以在抛出异常的地方放置一个断点,然后进行调查。
  • 请接受。
  • @OwnageIsMagic 抱歉,我没有看到这个问题,因为我最后的解决方法是将 C++ 开发切换到 Linux。

标签: c++ windows visual-studio-code windows-7 gdb


【解决方案1】:

有关catch 的更多信息,请参阅https://sourceware.org/gdb/onlinedocs/gdb/Set-Catchpoints.html#Set-Catchpoints

添加到launch.json:

{
    ...
    "setupCommands": [
        /*{
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        },*/
        {
            "description": "Enable break on all exceptions",
            "text": "catch throw",
            "ignoreFailures": true
        }
    ],
    ...
}

【讨论】:

  • 我可以两者兼得吗? (漂亮的打印似乎可以更好地启用 stl 调试)
  • @RanCohen 没有什么是真的;一切都被允许
  • 我在文本字段中都试过了。它不起作用。你能举个例子说明我如何同时拥有两者吗?
  • @RanCohen 只需删除 /**/
猜你喜欢
  • 2010-12-30
  • 2011-09-11
  • 2020-04-14
  • 2010-11-23
  • 2021-04-17
  • 2013-06-02
  • 1970-01-01
  • 2019-10-06
  • 2020-03-02
相关资源
最近更新 更多