I know that CppUnit will handle the exceptions through:
CPPUNIT_ASSERT_THROW(expression,ExceptionType);
Can anybody explain theoretically how CppUnit will handle the exception?
I know that CppUnit will handle the exceptions through:
CPPUNIT_ASSERT_THROW(expression,ExceptionType);
Can anybody explain theoretically how CppUnit will handle the exception?
Edit: I've upvoted Michael Anderson's answer, since he is more specific about the exact code from CppUnit, while mine is a more general answer.
In pseudocode, it'll be something like this:
try
{
// Test code that should throw
}
catch(ExceptionType e)
{
// Correct exception - handle test success
return;
}
catch(...)
{
// Wrong exception, handle test failure.
return;
}
// No exception, handle test failure.
return;
Reporting of test failures in CppUnit is done through throwing of a custom exception type. We'll call that CppUnitException here for simplicity.
CPPUNIT_ASSERT_THROW is a macro that will expand to something that is essentially this:
try
{
expression;
throw CppUnitException("Expected expression to throw");
}
catch( const ExceptionType & e )
{
}
If expression throws (as we'd expect it to) we fall into the catch block, which does nothing, without throwing the CppUnit exception.
If it does not throw we will hit the line of code that throws the CppUnitException which will trigger a test failure.
Of course CppUnit will make those macros a bit fancier to catch line and file information, but that is the general gist of how it works.