I have this constructor that throws an exception
GenericSocket::GenericSocket(const string& hostname, const string& servname): _hostname(hostname), _servname(servname) { initHints(); int rv; if((rv = getaddrinfo(_hostname.c_str(), _servname.c_str(), &_hints, &_servinfo)) != 0) { throw GenericSocketException(); } }
initHints () executes memset _hints and sets some variables.
I am testing it using the Google test platform as follows:
TEST(CreateObject2, getaddrinfoException) { mgs_addrinfo_return = 1; ASSERT_THROW(new GenericSocket("testhost", "4242"), GenericSocketException); }
Kernel dump test failed:
[ RUN ] CreateObject2.getaddrinfoException socket creation failed terminate called after throwing an instance of 'common::GenericSocketException' what(): Socket creation failed [1] 43360 abort (core dumped) ./bin/test_common
Besides the fact that I don’t know exactly what will go wrong, I suspect that some uninitialized object is being deleted (?), It seems that a lot is happening under the hood, so I started wondering if it is good practice to throw an exception in the constructor . Perhaps it is better to include this function in another function that I can call after creating the object and handle the exception later?
source share