I've got a particularly ornery piece of network code. I'm using asio but that really doesn't matter for this question. I assume there is no way to unbind a socket other than closing it. The problem is that open()
, bind()
, and listen()
can all throw a system_error
. So I handled the code with a simple try/catch
. The code as written in broken.
using namespace boost::asio;
class Thing
{
public:
ip::tcp::endpoint m_address;
ip::tcp::acceptor m_acceptor;
/// connect should handle all of its exceptions internally.
bool connect()
{
try
{
m_acceptor.open( m_address.protocol() );
m_acceptor.set_option( tcp::acceptor::reuse_address(true) );
m_acceptor.bind( m_address );
m_acceptor.listen();
m_acceptor.async_accept( /*stuff*/ );
}
catch( const boost::system::system_error& error )
{
assert(acceptor.is_open());
m_acceptor.close();
return false;
}
return true;
}
/// don't call disconnect unless connect previously succeeded.
void disconnect()
{
// other stuff needed to disconnect is ommited
m_acceptor.close();
}
};
The error is if the socket fails to connect it will try to close it in the catch block and throw another system_error about closing an acceptor that has never been opened.
One solution is to add an if( acceptor.is_open() )
in the catch block but that tastes wrong. Kinda like mixing C
-style error checking with c++
exceptions. If I where to go that route, I may as well use the non-throwing version of open()
.
boost::system::error_code error;
acceptor.open( address.protocol, error );
if( ! error )
{
try
{
acceptor.set_option( tcp::acceptor::reuse_address(true) );
acceptor.bind( address );
acceptor.listen();
acceptor.async_accept( /*stuff*/ );
}
catch( const boost::system::system_error& error )
{
assert(acceptor.is_open());
acceptor.close();
return false;
}
}
return !error;
Is there an elegant way to handle these possible exceptions using RAII and try/catch
blocks?
Am I just wrong headed in trying to avoid if( error condition )
style error handling when using exceptions?