在调试我的代码时,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
}
答案 0 :(得分:2)
有关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
}
],
...
}