я создал класс следующего исключения (а):Не может поймать мое исключение
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;
};
}
Реализация (ы):
InternalException::InternalException(const std::string& msg) : _msg(msg) { }
InternalException::~InternalException() throw() { };
const char* InternalException::what() const throw() {
return this->_msg.c_str();
}
кидает исключение, как это:
throw json::InternalException("Cannot serialize uninitialized nodes.");
Я хотел протестировать поведение исключения в тесте Boost :: Test:
// [...]
BOOST_CHECK_THROW(json::write(obj), json::InternalException); //will cause a json::InternalException
Однако тест завершается, когда исключение происходит, как если бы не было попытки ... уловить.
Если я сделаю попытку ... поймать явным и окружить json::write()
звонок с try{ json.write(obj); }catch(const json::InternalException& ex){}
или даже try{json.write(obj);}catch(...){}
, я получаю то же поведение. Исключение возникает, но я не могу его поймать ни на что.
Выход я получаю следующее:
terminate called after throwing an instance of 'json::InternalException'
what(): Cannot serialize uninitialized nodes.
Что я здесь делаю неправильно?
Может быть любая причина. Обычно отсутствует компиляция. –
Вы уверены, что исключение поднято? Другие примечания к вашему коду, избегайте указателя throw (или используйте noexcept, если ваш компилятор его поддерживает) и извлекаются из std :: runtime_error, поэтому вам не нужно писать логику строки 'what' – Geoffroy
. Вы не включили код для 'write', но моя ставка заключается в том, что вы просто бросаете другое исключение. –