Reputation: 5539
I've created the following exception class(es):
namespace json {
/**
* @brief Base class for all json-related exceptions
*/
class Exception : public std::exception { };
/**
* @brief Indicates an internal exception of the json parser
*/
class InternalException : public Exception {
public:
/**
* @brief Constructs a new InternalException
*
* @param msg The message to return on what()
*/
InternalException( const std::string& msg );
~InternalException() throw ();
/**
* @brief Returns a more detailed error message
*
* @return The error message
*/
virtual const char* what() const throw();
private:
std::string _msg;
};
}
Implementations(s):
InternalException::InternalException( const std::string& msg ) : _msg( msg ) { }
InternalException::~InternalException() throw () { };
const char* InternalException::what() const throw() {
return this->_msg.c_str();
}
I throw the exception like this:
throw json::InternalException( "Cannot serialize uninitialized nodes." );
I wanted to test the exception-throwing behaviour in a Boost::Test unit test:
// [...]
BOOST_CHECK_THROW( json::write( obj ), json::InternalException ); //will cause a json::InternalException
However, the test exits when the exception occurs as if there was no try...catch.
If I make the try...catch explicit and surround the json::write()
call with try{ json.write(obj); }catch(const json::InternalException& ex){}
or even try{json.write(obj);}catch(...){}
, I get the same behaviour. The exception is raised, but I can't catch it no matter what.
The output I get is the following:
terminate called after throwing an instance of 'json::InternalException'
what(): Cannot serialize uninitialized nodes.
What am I doing wrong here?
Upvotes: 1
Views: 704
Reputation: 5539
I found it. I figured it out while trying to throw an SSCCE together for you guys. I had json::write()
declared with a throw specifier, but didn't include json::InternalException
.
Adjusting the throw specifier to the correct exception now lets me actually catch it. Thanks for all the hints.
Upvotes: 1