Here is my code-
DBClientBase *conn = NULL;
string err_msg;
ConnectionString cs = ConnectionString::parse(connString, err_msg);
if (!cs.isValid()) {
throw "bad: " + err_msg;
}
try {
conn = cs.connect(err_msg);
}
catch (DBException &e) {
cout << "caught " << err_msg << endl;
return 1;
}
if (!conn) {
cout << "Unable to connect to DB" << endl;
return 1;
}
I would expect MongoDB to throw exception in case DB is not reachable. However, I am finding that if (!conn) is getting satisfied.
Why
catch (DBException &e) {
cout << "caught " << err_msg << endl;
return 1;
}
block isn't working?
From the current trunk source, ConnectionString::connect only seems to throw an exception when the string itself was invalid (and you already know that it was not, from your first conditional statement).
It just returns a NULL pointer and sets errMsg in all other cases.
In your defence, I couldn't find this documented anywhere at all; a very basic example of connect was all I could locate.
string err_msg;
ConnectionString cs = ConnectionString::parse(connString, err_msg);
if (!cs.isValid()) {
throw "bad: " + err_msg;
}
DBClientBase* conn = cs.connect(err_msg);
if (!conn) {
cout << "Unable to connect to DB: " << err_msg << endl;
return 1;
}
Related
I don't have much knowledge of exception.
I have two files task.cpp, main.cpp. Inside task.cpp file, I have a lot try/catch statements. I want to remove all the generic handlers for things like ..., std::exception etc., they are just printing the error, and I want to create just a single error handler of this kind and reside it inside main.cpp. How can I do it ?
These are some part of task.cpp & main.cpp file.
I want to remove catch (...) , catch (Exception const& _exception) from task.cpp file as they are used at multiple steps and create repetitive code
try
{
if (!stack.parseAndAnalyze(src.first, src.second))
successful = false;
else
stack.optimize();
}
catch (Exception const& _exception)
{
serr() << "Exception in assembler: " << boost::diagnostic_information(_exception) << endl;
return false;
}
catch (std::exception const& _e)
{
serr() <<
"Unknown exception during compilation" <<
(_e.what() ? ": " + string(_e.what()) : ".") <<
endl;
return false;
}
catch (...)
{
serr() << "Unknown exception in assembler." << endl;
return false;
}
}
if (_language != yul::AssemblyStack::Language::Ewasm && _targetMachine == yul::AssemblyStack::Machine::Ewasm)
{
try
{
stack.translate(yul::AssemblyStack::Language::Ewasm);
stack.optimize();
}
catch (Exception const& _exception)
{
serr() << "Exception in assembler: " << boost::diagnostic_information(_exception) << endl;
return false;
}
catch (std::exception const& _e)
{
serr() <<
"Unknown exception during compilation" <<
(_e.what() ? ": " + string(_e.what()) : ".") <<
endl;
return false;
}
catch (...)
{
serr() << "Unknown exception in assembler." << endl;
return false;
}
sout() << endl << "==========================" << endl;
sout() << endl << "Translated source:" << endl;
sout() << stack.print() << endl;
}
try
{
object = stack.assemble(_targetMachine);
object.bytecode->link(m_options.linker.libraries);
}
catch (Exception const& _exception)
{
serr() << "Exception while assembling: " << boost::diagnostic_information(_exception) << endl;
return false;
}
catch (std::exception const& _e)
{
serr() << "Unknown exception during compilation" << (
_e.what() ? ": " + string(_e.what()) : "."
) << endl;
return false;
}
catch (...)
{
serr() << "Unknown exception while assembling." << endl;
return false;
}
And catch inside main.cpp file like this:
int main(int argc, char** argv)
{
try
{
setDefaultOrCLocale();
solidity::frontend::CommandLineInterface cli(cin, cout, cerr);
if (!cli.parseArguments(argc, argv) || !cli.readInputFiles() || !cli.processInput() || !cli.actOnInput())
return 1;
return 0;
}
catch (boost::exception const& _exception || Exception const& _exception || InternalCompilerError const& _exception
|| smtutil::SMTLogicError const& _exception)
{
cerr << "Uncaught exception" << boost::diagnostic_information(_exception) << endl;
return 1;
}
catch (std::exception const& _e)
{
cerr() << "Uncaught exception" << (_e.what() ? ": " + string(_e.what()) : ".") << endl;
return 1;
}
catch (Exception const& _exc)
{
cerr() << string("Failed to import AST: ") << _exc.what() << endl;
return 1;
}
catch (...)
{
cerr << "Uncaught exception" << endl;
return 1;
}
}
I'm quite noob yet in OOP and is my first time handling exceptions and Templates, may be I planned the function in a wrong way...
But I would like to know what should I return in this case if the execution goes wrong and the exception is thrown.... what kind of data error return in a function returning a Template?
Sorry if I am not clear enough, english is not my mothertongue...
template<typename T>
const T& List<T>::Next()
{
try
{
if (_actual->getNext() == NULL)
throw out_of_range("No next elements, list out of bounds");
else
{
_actual = _actual->getNext();
_Position++;
return _actual->getData();
}
}
catch (out_of_range &e)
{
cerr << "Error, " << e.what() << endl << "Position: " << _Position << " Elements: " << _Elements << endl;
}
// <--- what should I return here?? return NULL;? return 0;? return <T> thrash;??
}
If there's nothing to return then there's nothing to return.
Let the exception propagate, either by not catching it here, or by re-throwing it after your cerr statement with the throw statement:
catch (out_of_range &e)
{
cerr << "Error, " << e.what() << endl
<< "Position: " << _Position
<< " Elements: " << _Elements << endl;
throw;
}
Your next question will be how to handle the exception in the calling scope. :)
But at least you won't have to worry about return values any more.
I try to cascade exception in Poco.
void debug() {
try {
...
xmlFile.parseDocument(*_sim);
...
}
} catch (Poco::Exception& error) {
std::cout << "I'm here" << endl;
std::cout << "Error : " << error.displayText() << std::endl;
}
}
void XMLParser::parseDocument(Manager &manager) {
...
try {
Poco::XML::NodeList* policyList = root->childNodes();
for (uint node=0; node < policyList->length(); node++)
if (policyList->item(node)->hasChildNodes())
manager.insertRule(parseRule(node, policyList->item(node)));
} catch(Poco::Exception& error) {
std::cout << "Error : " << error.displayText() << std::endl;
error.rethrow();
}
}
Rule* XMLParser::parseRule(int flowID, Poco::XML::Node* rule) throw() {
....
if (tLink._srcPort < 0)
throw new Poco::Exception("Source Port isn't valid");
....
}
The deepest exception are thrown, but it does not continue to outer functions.
The program is terminated. Why?
You throw a Poco::Exception pointer so you can not catch it by reference.
Remove 'new'. This should work:
....
if (tLink._srcPort < 0)
throw Poco::Exception("Source Port isn't valid");
....
I have a class to connect MySQL database. This class has 4 methods. (insert, getResults etc.) I don't want to create database connection in every method. So i want an init() when we create this object. Is connection pool solution of my problem? How can i solve?
Have 4 methods like that:
bool DataAccessObject::getResults(short int data, std::vector<FaceRecord>* rec)
{
// DataAccessObject *temp = new DataAccessObject();
bool ret = false;
try{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *prepStmt;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
std::stringstream s;
s << "SELECT * FROM Amts WHERE "<< data <<" = "<< data <<"";
prepStmt = con->prepareStatement (s.str());
res = prepStmt->executeQuery();
while(res->next()){
tempFR.uuId = res->getInt64("uuId");
tempFR.cameraNo = res->getInt("cameraNo");
tempFR.age = res->getInt("age");
tempFR.gender = res->getInt("gender");
tempFR.time = res->getString("time");
tempFR.image = res->getString("image");
rec->push_back(tempFR);
}
//return true;
ret = true;
}
catch (sql::SQLException &e)
{
std::cout << "# ERR: SQLException in " << __FILE__;
std::cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << std::endl;
std::cout << "# ERR: " << e.what();
std::cout << " (MySQL error code: " << e.getErrorCode();
std::cout << ", SQLState: " << e.getSQLState() << " )" << std::endl;
}
return ret;
}
You can use the C++ Singleton design pattern so that your init is called only once when you create it.
I am having a problem while creating a client program that sends requests. The request are using keep alive TCP HTTP connections. When a connection is closed(due to timeout or max being hit), I try and start a new connection if none are available, and resend the request. The connect works fine however, when I try and send the write, nothing is sent(according to Wireshark), but my error code for the write was a success. The receiving server does not receive any information either. Here is the main parts of my code:
void request_handler::send_1(std::vector<std::string> *bid_vector, std::string request_path, boost::mutex *bids_mutex)
{
try
{
boost::asio::streambuf request;
std::ostream request_stream(&request);
std::string reply_information;
request_stream << "GET /tests HTTP/1.1\r\n";
request_stream << "Host: 10.1.10.160\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: keep-alive\r\n\r\n";
server1_mutex_.lock();
if(server1_available_map_.size() == 0)
{
server1_mutex_.unlock();
persistent_connection *new_connection = new persistent_connection("10.1.10.160","80");
if(new_connection->send(request, reply_information))
{
server1_mutex_.lock();
server1_available_map_[new_connection->get_id()] = new_connection;
server1_mutex_.unlock();
}
}
else
{
persistent_connection *current_connection = (*(server1_available_map_.begin())).second;
server1_available_map_.erase(current_connection->get_id());
server1_mutex_.unlock();
int retry_counter = 20;
while(!current_connection->query_rtb(request, reply_information) && --retry_counter != 0)
{
delete current_connection;
server1_mutex_.lock();
if(server1_available_map_.size() == 0)
{
server1_mutex_.unlock();
current_connection = new persistent_connection("10.1.10.160","80");
}
else
{
current_connection = (*(server1_available_map_.begin())).second;
server1_available_map_.erase(current_connection->get_id());
server1_mutex_.unlock();
}
}
//Could not connect to 20 connections
if(retry_counter == 0)
{
Log::fatal("Could not connect in 20 tries");
delete current_connection;
return;
}
server1_mutex_.lock();
server1_available_map_[current_connection->get_id()] = current_connection;
server1_mutex_.unlock();
}
bids_mutex->lock();
bid_vector->push_back(reply_information);
bids_mutex->unlock();
}
catch(boost::thread_interrupted& e)
{
std::cout << "before cancel 1" << std::endl;
return;
}
catch(...)
{
std::cout << "blah blah blah" << std::endl;
}
}
And my persistent_connection class
persistent_connection::persistent_connection(std::string ip, std::string port):
io_service_(), socket_(io_service_), host_ip_(ip)
{
boost::uuids::uuid uuid = boost::uuids::random_generator()();
id_ = boost::lexical_cast<std::string>(uuid);
boost::asio::ip::tcp::resolver resolver(io_service_);
boost::asio::ip::tcp::resolver::query query(host_ip_,port);
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
boost::asio::ip::tcp::endpoint endpoint = *iterator;
socket_.async_connect(endpoint, boost::bind(&persistent_connection::handler_connect, this, boost::asio::placeholders::error, iterator));
io_service_.run();
}
void persistent_connection::handler_connect(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{
if(ec)
{
std::cout << "Couldn't connect" << ec << std::endl;
return;
}
else
{
boost::asio::socket_base::keep_alive keep_option(true);
socket_.set_option(keep_option);
std::cout << "Connect handler" << std::endl;
}
}
bool persistent_connection::send(boost::asio::streambuf &request_information, std::string &reply_information)
{
std::cout << "DOING QUERY in " << id_ << std::endl;
boost::system::error_code write_ec, read_ec;
try
{
std::cout << "Before write" << std::endl;
boost::asio::write(socket_, request_information, write_ec);
std::cout << write_ec.message() << std::endl;
}catch(std::exception& e)
{
std::cout << "Write exception: " << e.what() << std::endl;
}
if(write_ec)
{
std::cout <<"Write error: " << write_ec.message() << std::endl;
return false;
}
boost::array<char,8192> buf;
buf.assign(0);
try
{
std::cout << "Before read" << std::endl;
boost::asio::read(socket_, boost::asio::buffer(buf), boost::asio::transfer_at_least(1), read_ec);
std::cout << read_ec.message() << std::endl;
}catch(std::exception& e)
{
std::cout << "Read exception: " << e.what() << std::endl;
}
if(read_ec)
{
std::cout << "Read error: " << read_ec.message() << std::endl;
return false;
}
reply_information = buf.data();
return true;
}
std::string persistent_connection::get_id()
{
return id_;
}
The path for this to happen is if server1_available_map_.size() > 0, and if the while executes, and fails. And then if the size == 0 on the second server1_available_map_.size();
The output for the call is:
DOING QUERY in 69a8f0ab-2a06-45b4-be26-37aea6d93ff2
Before write
Success
Before read
End of file
Read error: End of file
Connect handler
DOING QUERY in 4eacaa96-1040-4878-8bf5-c29b87fa1232
Before write
Success
Before read
Which shows that the first connection gets an end of file(connection closed by server on other end). The second connection connects fine(Connect handler message), and the query is executed in the second connection(different id), and the write is apparently successful, and the program hangs on the read(because there is nothing to read).
Does anyone have any idea why this would be happening? Is there something I seem to be doing wrong?
Thank you
It looks like you are passing the same boost::asio::streambuf to multiple write calls.
boost::asio::write(socket_, request_information, write_ec);
The contents of the buffer are consumed by the first call to boost::asio::write. This effectively empties the buffer so that there is nothing left to send. Pass a const string if you want to use the same buffer for multiple writes.