mysql_real_connect阻塞_mysql_real_connect崩溃、未经处理的异常
mysql_real_connect崩潰、未經(jīng)處理的異常
背景
近期客戶(hù)測(cè)試軟件,功能里有mysql連接問(wèn)題,在mysql連接失敗時(shí),客戶(hù)機(jī)器上出現(xiàn)“已停止工作”界面,而我機(jī)器上軟件直接退出沒(méi)有提示自動(dòng)關(guān)閉。
查找分析
因?yàn)槭且恢庇玫拇a,和網(wǎng)上代碼對(duì)比mysql_real_connect也用法一樣,想不到哪里錯(cuò)誤,就先加上mysql的option處理代碼,類(lèi)似如下。
unsigned int timeout = 5;
int ret = mysql_options(_connectionHandlerPtr, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&timeout);
if (ret) {
throw DataBaseError("mysql參數(shù)設(shè)置失敗");
return;
}
if (NULL == mysql_real_connect(_connectionHandlerPtr, server.c_str(), user.c_str(), password.c_str(), database.c_str(), import, NULL, 0))
{
throw DataBaseError("連接數(shù)據(jù)庫(kù)失敗");
return;
}
char value = 1;
mysql_options(_connectionHandlerPtr, MYSQL_OPT_RECONNECT, &value);
加了還是不行,不過(guò)用代碼調(diào)試運(yùn)行報(bào)“未經(jīng)處理的異?!?#xff0c;就查try,catch代碼,把catch里括號(hào)里參數(shù)類(lèi)型里*去掉,“未經(jīng)處理的異常”不報(bào)了,mysql_real_connect也不崩潰了。
未經(jīng)處理的異常
從msdn里查下try catch的描述。
try {
// code that could throw an exception
}
[ catch (exception-declaration) {
// code that executes when exception-declaration is thrown
// in the try block
}
[catch (exception-declaration) {
// code that handles another exception type
} ] . . . ]
// The following syntax shows a throw expression:
throw [expression]
exception-declaration不是無(wú)變化的意思,它是和throw的值類(lèi)型有關(guān)的,如果throw "abc",則catch里可以帶*號(hào)是char*,如果throw CXXError,就要catch(CXXError e),拋出什么類(lèi)型就要catch什么類(lèi)型,如果類(lèi)型不一致,則會(huì)報(bào)“未經(jīng)處理的異?!?。
mysql_real_connect崩潰
mysql_real_connect連接失敗是會(huì)正常上報(bào),代碼對(duì)應(yīng)處理時(shí)會(huì)throw,是throw的問(wèn)題,mysql_real_connect沒(méi)有問(wèn)題。
總結(jié)
以上是生活随笔為你收集整理的mysql_real_connect阻塞_mysql_real_connect崩溃、未经处理的异常的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: stol函数在linux下使用,Linu
- 下一篇: 7安装sql cent os serve