Exception not thrown/caught

Hi,

I have created exceptions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdexcept>
#include<string>

class CompteException: public std::runtime_error
{
public:
	CompteException(const std::string& p_raison);
};

class CompteAbsentException: public CompteException
{
public:
	CompteAbsentException(const std::string& p_raison);
};


1
2
3
4
5
6
7
8
9
10
11
#include"CompteException.h"

using namespace std;

CompteException::CompteException(const string& p_raison): runtime_error(p_raison)
{
}

CompteAbsentException::CompteAbsentException(const string& p_raison): CompteException(p_raison)
{
}


which I have used in this piece of code (a method of a class with all necessary header files included):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Client::supprimerCompte(unsigned int 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...

Any help would be Greatly appreciated. Thanks.
Are you sure the if condition compteEstDejaPresent(p_noCompte) is ever false?
Hi Peter87,

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...

EDIT: I changed my "catch" block to:
1
2
3
4
catch(CompteAbsentException const& p_exception)
{
	cout << "Test exception";// L'exception est attrapée.
}


and got the output "Test exception" in the console. I really don't understand what is going on...
Last edited on
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.
Last edited on
Hi,

GoogleTest uses macros to do unit tests on methods/functions. For instance, I used:

ASSERT_THROW(t_client.supprimerCompte(37), CompteAbsentException);

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 reason is that supprimerCompte(...) does not throw an exception because it is actually handled internally by the function.
You mean, the exception treatment is done inside suprimerCompte(...) and it's not right? How come?

Thank you
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)
Last edited on
Thank you very much, that solved it for me!
Topic archived. No new replies allowed.