简体   繁体   中英

How to set breakpoint in catch block? (c++)

There is something strange happening when I try to debug application. Simply the debugger does not stop on breakpoints when I set breakpoints in catch portion of try-catch block.

Here is an example.

try {
    throw std::overflow_error("test");
} catch (...) {
    qDebug() << "caught"; // HERE, I SET BREAKPOINT ON THIS LINE
}

It prints the "caught" on screen when exception occurs but it does not stop on this line. (If you ever wonder; Yes, I'm building app in Debug mode and running in Debug mode)

Am I suffering from lack of fundamental knowledge about how gdb works? (I mean maybe it does not stop because breakpoints in catch portion does not work)

Any help would be greatly appreciated.

Thanks.

To catch an exception in IDE, you need issue gdb commands directly in gdb console. Here's link how to get into gdb console in Qt Create IDE: Accessing gdb console in Qt-Creator

Once you're the type

catch throw 

to stop when your program throws an exception or

catch catch 

to stop in the catch block.

If you need to catch a specific library exception, read this thread: GDB: How to break when a specific exception type is thrown?

for people using LLDB,

# set on both throw and catch
breakpoint set -E C++ -h true
# or on catch
b __cxa_begin_catch
# or on throw
b __cxa_throw

while will set breakpoints on throw and catch.

@ben sen, I guess any opinization may result in such a behavior. There are many ways how those options can be specified (via environment variables aka CFLAGS or via IDE options to the project), but they all result to some particular -O option given to the compiller command line. Even if nothing is given at all, please check what is the default optimization for your compiller. My proposal would be to explicitely give -O0 to the compiller and check that no other -O options are supplied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM