Quantcast
Channel: MySQL Forums - Connector/C++
Viewing all articles
Browse latest Browse all 527

How to prevent program from crashing if mysql host is not reachable (no replies)

$
0
0
I'm using the MySQL Connector for c++

I'm trying to connect to a remote MySQL server which happens to be offline at the moment and I noticed that my program throws an exception and aborts!!
It is OK that it could not connect however I'd like to keep my program running.
because in case of a failed connection I'd like to try connecting to a different server but I cant if the program crashes after the first attempt.

Is there a way to do prevent this crash?


Thanks,


here is the connection code:

Connection * conn;
mysql::MySQL_Driver *driver;

try
{
driver = mysql::get_mysql_driver_instance();
conn = driver->connect("host1", "user", "pswd");
} catch (SQLException &e) {
cout << "ERROR: SQLException in " << __FILE__;
cout << " (" << __func__<< ") on line " << __LINE__ << endl;
cout << "ERROR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << ")" << endl;
}
catch (std::runtime_error &e){
cout << "ERROR: runtime_error in " << __FILE__;
cout << " (" << __func__ << ") on line " << __LINE__ << endl;
cout << "ERROR: " << e.what() << endl;
}

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>