简体   繁体   English

如何在catch块中设置断点? (c++)

[英]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.当我在 try-catch 块的 catch 部分设置断点时,调试器不会在断点处停止。

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) (如果您想知道;是的,我正在 Debug 模式下构建应用程序并在 Debug 模式下运行)

Am I suffering from lack of fundamental knowledge about how gdb works?我是否缺乏有关 gdb 工作原理的基本知识? (I mean maybe it does not stop because breakpoints in catch portion does not work) (我的意思是它可能不会停止,因为 catch 部分中的断点不起作用)

Any help would be greatly appreciated.任何帮助将不胜感激。

Thanks.谢谢。

To catch an exception in IDE, you need issue gdb commands directly in gdb console.要在 IDE 中捕获异常,您需要直接在 gdb 控制台中发出 gdb 命令。 Here's link how to get into gdb console in Qt Create IDE: Accessing gdb console in Qt-Creator这是如何在 Qt Create IDE 中进入 gdb 控制台的链接: 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.停在 catch 块中。

If you need to catch a specific library exception, read this thread: GDB: How to break when a specific exception type is thrown?如果您需要捕获特定的异常,请阅读此线程: GDB:抛出特定异常类型时如何中断?

for people using LLDB,对于使用 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. while 会在 throw 和 catch 上设置断点。

@ben sen, I guess any opinization may result in such a behavior. @ben sen,我想任何意见都可能导致这种行为。 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.有很多方法可以指定这些选项(通过环境变量又名 CFLAGS 或通过项目的 IDE 选项),但它们都会导致某些特定的 -O 选项提供给编译器命令行。 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.我的建议是明确地将 -O0 提供给编译器并检查是否提供了其他 -O 选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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