1

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.

  • 3
    Are you sure that `cv::threshold` throws? – Mohamad Elghawi Feb 03 '16 at 12:17
  • * If your code throwing exception then only it will come in catch block.So make sure that. – Sagar Patel Feb 03 '16 at 12:21
  • @MohamadElghawi you're right to ask this question. I didn't mentioned it sorry, it is just a pseudo code. It doesn't have to be threshold method. – benjamin button Feb 03 '16 at 12:23
  • @SagarPatel I just edited my question. – benjamin button Feb 03 '16 at 12:23
  • Sometimes a debugger stops at "next line to be executed". I think you should supply us more details on how do you debug. – Lukáš Bednařík Feb 03 '16 at 12:45
  • @LukášBednařík thanks for your reply. I'm using Qt Creator IDE and setting breakpoints by using this GUI (I mean just clicking line number sets breakpoint.) Then push run in debug. (or F5). Also I'm using gdb version 7.7.1 – benjamin button Feb 03 '16 at 12:59
  • 2
    use "catch catch, catch throw" in gdb – Dmitry Alexeyev Feb 03 '16 at 14:51
  • One additional hint. Please check whether the compiler optimization is enabled. If yes, such constructions can be optimized out, and then the debugger has no actual commands behind some specific lines of C code, or even whole functions or their blocks. In that case try to disable optimization, recompile and try again. – dmi Feb 03 '16 at 14:54
  • @DmitryAlexeyev thanks for reply but I don't think I got your message. I'm using IDE to debug application, not debugging directly in terminal. (I mean by using gdb command) – benjamin button Feb 03 '16 at 16:47
  • @dmi thanks for the reply. Do you have any idea which level of optimization can cause this? Or which level of optimization should I use to make gdb stop in catch portions of try-catch block? – benjamin button Feb 03 '16 at 16:48
  • 1
    That's how to access gdb console in Qt Creator: http://stackoverflow.com/questions/4592643/accessing-gdb-console-in-qt-creator You need to type "catch catch" or "catch throw" (or both) to stop program execution on exception. – Dmitry Alexeyev Feb 04 '16 at 07:13
  • @DmitryAlexeyev you're f**in awesome. thanks. adding "catch throw" command to gdb on startup resolved the problem. can you add this as an answer? so I can approve it. – benjamin button Feb 04 '16 at 09:39
  • @bensen Sure :) Glad it helped. – Dmitry Alexeyev Feb 05 '16 at 10:08

3 Answers3

3

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?

Community
  • 1
  • 1
Dmitry Alexeyev
  • 207
  • 1
  • 4
2

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.

Izana
  • 2,537
  • 27
  • 33
0

@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.

dmi
  • 1,424
  • 1
  • 9
  • 9
  • I appologize, I put my comment to a wrong field - please don't treat it as an answer, but as a comment. Thank you. – dmi Feb 03 '16 at 17:00
  • thanks. I disabled optimizations by -O0 and tried to recomplime my application. Again it did not stop in catch block. – benjamin button Feb 03 '16 at 20:42