【问题标题】:Exception sometimes not caught using C++ / GCC / Linux使用 C++ / GCC / Linux 有时无法捕获异常
【发布时间】:2021-12-08 00:04:30
【问题描述】:

偶尔我会遇到一段代码没有捕获到异常。到目前为止,我不知道可能的原因是什么。

以下面这段代码为例。

  • 打印日志消息“正在初始化”。
  • 一些来自 module->Init() 的日志信息被打印出来
  • module->init() 抛出异常
  • 不打印 catch 块中的日志消息

应用程序终止

log::trace( "Initializing" );
try
{
   module->Init();
}
catch( const std::exception& e )
{
    log::error( "Error initializing module:{}", e.what());
}
catch( ... )
{
    log::error( "Unknown exception initializing module" );
}

dmesg:
[    9.557049] terminate called after throwing an instance of '
[    9.562655] std::runtime_error
[    9.565715] '
[    9.567350]   what(): 
followed by the error message passed to what() which was the generated  exception message in init.  
  • 异常处理在同一应用程序的其他部分按预期工作。
  • 从主线程抛出异常。
  • 带有构建系统的嵌入式 ARM 平台。所有库都使用相同的编译器进行编译。
  • 编译器使用 GCC 8.2.0

有什么我不知道的,可能会导致无法捕获异常吗?

【问题讨论】:

  • 可以log::error扔吗?
  • "后跟传递给 what() 的错误消息"。这可能表明/暗示异常的起源。
  • 一些析构函数可以抛出(在展开期间)吗?
  • 请提供完整的、可复现的代码示例。
  • 你能在调试器下运行代码吗?您应该能够在throw 调用(gdb 中的catch throw)上放置一个通用断点,这可能会提供更多信息。

标签: c++ exception gcc arm


【解决方案1】:

实际上忽略了头文件中的一些内容。该函数被标记为“noexcept”,它实际上在引发异常时导致终止。

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    相关资源
    最近更新 更多