Solved: How to handle exception with Boost Asio and C/C++
At the moment, i’m using Boost Asio in order to connect to a server via TCP.
I use a conditional case to decide if the application has to start or not a connection with the server; it works great but the problem is that if i try to connect to the server when the server is down then the application crash giving this error:
terminate called after throwing an instance of ‘boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector >’
what(): Connection refused
This is the code i’m using:
case CONNECTION: // Connect to the server using boost::asio::ip::tcp; boost::asio::io_service io_service; tcp::resolver resolver(io_service); tcp::resolver::query query(tcp::v4(), server, boost::lexical_cast(porta)); tcp::resolver::iterator iterator = resolver.resolve(query); tcp::socket s(io_service); s.connect(*iterator);
I’d like to keep alive my application with its normal behavior and only prompt a warning about the connection failed.
How can i handle this exception?
I’m not very good in C++ but i think there is a way to manage the exception.
I hope you can help me.