c++ - GCC 程序不捕获异常

标签 c++ gcc exception-handling

我的项目包含共享库和 exe 客户端。我发现我自己从库中抛出的异常类没有被客户端 catch block 捕获,并且程序以“在抛出一个实例后调用终止...”消息终止。继续玩这个项目,我发现任何类型的异常都没有被捕获。例如,这个 catch 不起作用:

    try
    {
        m_pSerialPort = new boost::asio::serial_port(m_IoService, "non-existing-port");
    }
    catch(const boost::system::system_error& e)
    {
        // ...
    }

错误信息:

terminate called after throwing an instance of 
'boost::exception_detail::clone_impl
<boost::exception_detail::error_info_injector
<boost::system::system_error> >'
  what():  No such file or directory

GCC 版本为 4.4.1,Linux 操作系统。相同的代码在 Windows、MSVC 中成功运行。 什么原因可以阻止 GCC 程序正确捕获异常?

最佳答案

客户端 .exe 和共享库都应该与 libgcc 链接,以便跨越共享库边界。根据 GCC 手册:

... if a library or main executable is supposed to throw or catch exceptions, you must link it using the G++ or GCJ driver, as appropriate for the languages used in the program, or using the option -shared-libgcc, such that it is linked with the shared libgcc.

关于c++ - GCC 程序不捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2424836/

相关文章:

java - 为什么 Thread 的子类可以吞下 InterruptedException?

java - 如何在 Qt for android 中保持屏幕打开?

c++ - 无法打开源文件 "sys/types.h", "sys/stat.h"

c++ - 如果异常被禁用,所有功能都是 "noexcept"吗?

opencv - 编译 OpenCV 时出错, fatal error : stdlib. h: No such file or directory

c++ - 将 C++ 共享库链接到 C 程序时如何避免错误

plsql - 如何捕获 PL/SQL 脚本中的所有异常?

java - Jax-RS( Jersey )上下文中 WebApplicationException 和 WebServiceException 的区别

c++ - 确定谁打开了 TCP session

c++ - 顺序存储不同类型的对象