Original Post
I''m a procedural developer coming to terms with C++ and OOP. At the moment I''m torn between when to use exceptions and when to just return error codes. For example, I''m writing a C++ networking library that wraps Winsock. Currently my code for error detection is like this:
mSocket = socket(AF_INET, SOCK_STREAM, 0);
if (mSocket == HB_INVALID)
{
hbNetEngine::getInstance()->setError(getLastError());
closeConnection();
return HB_FALSE;
}
So I return the error to the application from the library and deal with it there.
But I also had this funky idea of implementing exceptions, so I included:
HBhost* hostent = gethostbyname(addr); ASSERT(hostent, NULL, "Error looking up remote hostname.");The ASSERT macro wraps try and catch, and executes a custom exception handler that basically cleanly shuts down the application an exits. The only drawback here is that the code doesn''t set the network engine error code, so that the application doesn''t really know what went wrong. Should I use a mix of the two, or stick with just one? I initially implemented everything using return codes, but now I''m not sure what to do! Thanks