VSCode on Windows:gdb 不会在 'throw' 上中断,但会在常规异常上中断

VSCode on Windows: gdb doesn't break on 'throw' but breaks on regular exceptions

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

我的环境:

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
        }

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