1
我正在使用我自己的Exception
類,它繼承自std::exception
。我很確定這門課沒什麼問題,因爲它一直都在努力。我試圖從構造函數拋出一個錯誤:導致中止的未捕獲的異常
DataBase::DataBase()
: _result(NULL)
{
mysql_init(&_mysql);
mysql_options(&_mysql,MYSQL_READ_DEFAULT_GROUP,"option");
if(!mysql_real_connect(&_mysql,"localhost","root","","keylogger",0,NULL,0))
throw Exception(__LINE__ - 1, __FILE__, __FUNCTION__, "Can't connect to DB");
}
這裏是我的try/catch塊:
int main(int, char **)
{
//[...]
Server server([...]); // DB is a private member in Server
try
{
server.fdMonitor();
}
catch (Exception &e)
{
std::cout << "Error: in " << e.file() << ", " << "function " << e.function()
<< "(line " << e.line() << ") : " << std::endl
<< "\t" << e.what() << std::endl;
}
return (1);
}
的問題是,從我的DB構造函數拋出的異常沒有被捕獲。這是中止消息:
terminate called after throwing an instance of 'Exception'
what(): Can't connect to DB
Aborted
任何想法? 在此先感謝。