奇怪 - mysql 的 sql::SQLException 没有被其类型捕获,而是被捕获为 std::exception 并成功转换回去
weird - mysql's sql::SQLException is not caught by its type, but is caught as std::exception and cast back successfully
我正在使用mysql c ++连接器和这个(有点简化的(代码。
try
{
statement->setString(1, word);
statement->executeUpdate();
}
catch( sql::SQLException& e )
{
// I don't get here
return sqlerrno_to_error_code( e.getErrorCode() );
}
catch( std::exception& e )
{
// I do get here and the cast works
sql::SQLException& sqle = (sql::SQLException&) e;
return sqlerrno_to_error_code( sqle.getErrorCode() );
}
连接器应该抛出 sql::SQLException,它派生自 std::exception,并具有一些其他方法,如 getErrorCode()
。
引发的异常在第二个catch
块中捕获,但可以成功强制转换为(并用作(sql::SQLException
。
更奇怪的是,不同可执行文件中的类似代码会按预期捕获sql::SQLException
。它们之间的区别在于,第一个位于加载了dlopen()
的共享对象(.so(中。
RHEL 5.7 32 位,gcc 4.1.2
请参阅 GCC 常见问题页面上的 dynamic_cast
、 throw
typeid
不使用共享库的说明。
因为您使用的是 dlopen()
,所以您需要将可执行文件与 -E
标志链接(或者如果g++
调用链接器,则将-Wl,-E
传递给g++
(并将 RTLD_GLOBAL
标志传递给 dlopen()
。
相关文章:
- 为重写std::exception的库生成swig接口时出错
- 什么是 std::exception::what() 以及为什么要使用它?
- 为什么程序员同时使用 std::bad_alloc 和 std::exception.是否 std::例外 仅是不够的
- 请参阅在 Visual Studio 2019 中捕获 std::exception 时对函数模板实例化消息的引用
- 自定义派生的 std::exception 类的 'what' 函数返回神秘的废话
- 派生自 std::exception 的类的赋值运算符
- std::exception :使用虚函数和继承与变量
- 这段 C++ 代码的奇怪行为(std::wcout 和 std::exception)
- 无法在"catch(const std::exception &ex)"中捕获 std::invalid_argument
- C++ CMake 构建错误:未定义对"boost::throw_exception(std::exception const&)"的引用
- 无法通过引用 std::exception 来捕获从 std::exception 派生的类
- 如何从 std::exception 到 std::nested_exception dynamic_cast?
- C++ catch(std::exception & e ) vs. catch(...)
- 为什么GCC可以编译std::exception("some error msg")而没有错误?
- 如何捕获 I/O 异常(确切地说是 I/O,而不是 std::exception)
- 在 CLI 中抛出 std::exception 会引发访问冲突
- 为什么 std::exception 会在 std::bad_alloc 之前捕获我的异常
- Use case for std::promise::set_{value|exception}_at_thread_e
- 为什么人们将虚拟关键字放在std :: exception :: what()的面前
- 矢量存储限制的向量 - 获得"Microsoft C++ exception: std::bad_alloc at memory location 0x0031650C."错误