Windows 上的 VSCode:gdb 不会在“抛出”时中断,但会在常规异常时中断

Sew*_*cca 7 c++ windows gdb windows-7 visual-studio-code

在调试我的代码时,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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

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.
Run Code Online (Sandbox Code Playgroud)

我的环境:

  • Windows 7 64 位
  • VSCode 1.40.2
  • 明GW

启动.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
        }
Run Code Online (Sandbox Code Playgroud)

Own*_*gic 10

有关详细信息,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
        }
    ],
    ...
}
Run Code Online (Sandbox Code Playgroud)

  • @RanCohen 只需删除“/*”和“*/”。 (2认同)