c++ - Windows 上的 VSCode : gdb doesn't break on 'throw' but breaks on regular exceptions

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

在调试我的代码时,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

  • 启动文件
            {
                "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
            }
    

    最佳答案

    有关 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
            }
        ],
        ...
    }
    

    关于c++ - Windows 上的 VSCode : gdb doesn't break on 'throw' but breaks on regular exceptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59276055/

    相关文章:

    windows - 在 Windows 上使用堆栈

    ios - 解析 Visual Studio 推送通知

    dart - 如何修复VSCode中dart:html的查找错误?

    c++ - 使用 Bison 构建 AST

    c++ - GLSL 1.20 : sending attribute causes segmentation fault

    c++ - 向 C++ 数组添加维度?

    c++ - 没有匹配函数调用 ‘Tree<int>::operator==(Tree<int>*&, Tree<int>*&) const’

    C++ 转换 Windows IAction

    visual-studio-code - 允许自动缩进二维数组(如表格)的编辑器或扩展

    javascript - 如何在 Visual Studio Code 中更改代码格式