在调试我的代码时,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.
我的环境:
启动文件
{
"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/