我正在使用帶有此(稍微簡化)代碼的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.它們之間的區別在于,第一個位于共享庫(.so)中,該共享庫已加載dlopen().
RHEL 5.7 32位,gcc 4.1.2