While debugging my code, gdb catches regular exceptions (such as dividing by zero) but not custom ones, thrown with throw
.
I'd expect vscode to jump where the exception is thrown, so i can investigate. Instead, the exception causes the application to terminate and the debugger to close.
I tested it with some dummy code:
compiled with: 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;
}
The output:
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.
My environment:
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
}
Visual Studio Code supports the following debuggers for C/C++ depending on the operating system you are using: Linux: GDB. macOS: LLDB or GDB.
Breakpoints can be toggled by clicking on the editor margin or using F9 on the current line. Finer breakpoint control (enable/disable/reapply) can be done in the Run and Debug view's BREAKPOINTS section.
Set breakpoints in source code To set a breakpoint in source code, click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.
For more info about catch
see https://sourceware.org/gdb/onlinedocs/gdb/Set-Catchpoints.html#Set-Catchpoints
add to 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
}
],
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With