void Client::supprimerCompte(unsignedint p_noCompte)
{
try
{
vector<Compte*>::iterator it;
if(compteEstDejaPresent(p_noCompte))
{
for(it = m_vComptes.begin(); (*it)->reqNoCompte() != p_noCompte; it++)
{
}
delete *it; // On désalloue la mémoire associée à ce pointeur sur le monceau avant la destruction du compte.
m_vComptes.erase(it);
}
else
{
throw CompteAbsentException("Le compte est inexistant!");
}
}
catch(CompteAbsentException const& p_exception)
{
// L'exception est attrapée.
}
}
Everything compiles fine. When I test my function, Google test says that no exception is thrown. I can't seem to find what is wrong with this code...
I have followed this function in the debugger and yes, the condition is false when I want it to be. When the debugger arrives at the "throw" line, it simply goes out of the function and does not go to "catch" block...
When I test my function, Google test says that no exception is thrown.
I don't know how "Google test" works (or what it is) but maybe it just means there are no uncaught exceptions.
When the debugger arrives at the "throw" line, it simply goes out of the function and does not go to "catch" block...
There is no code to run in the catch block so it probably just skips it all together. Put some print statement there and you will probably see that it works as it should.
Now t_client.supprimerCompte(37) does not meet the compteEstDejaPresent(p_noCompte) condition.
GoogleTest simply verifies that in that case, an exception of the type "CompteAbsentException" is thrown and gives you some output on the unit test result. My output is that the test failed, and that no exception was thrown. I just don't understand why in this case, no exception is detected...
The function does not throw an exception because you caught it with the try/catch. If you want the exception to be thrown outside the function you should remove the try/catch (or rethrow the exception from the catch block)