无法捕获我的异常
Can't catch my exception
我创建了以下异常类:
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
但是,当异常发生时,测试将退出,就好像没有尝试一样...抓住。
如果我尝试...捕获显式并用try{ json.write(obj); }catch(const json::InternalException& ex){}
甚至try{json.write(obj);}catch(...){}
包围json::write()
调用,我得到相同的行为。异常被提出,但无论如何我都抓不到它。
我得到的输出如下:
terminate called after throwing an instance of 'json::InternalException'
what(): Cannot serialize uninitialized nodes.
我在这里做错了什么?
我找到了。我在试图为你们一起举办 SSCCE 时想通了。我json::write()
用投掷说明符声明,但没有包括json::InternalException
.
现在,将抛出说明符调整为正确的异常可以让我实际捕获它。感谢您的所有提示。
相关文章:
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 为什么我尝试在 OpenGL 中加载的 3ds 文件会出现此未经处理的异常?
- 我正在尝试创建一个使用 c++ 中的参数包构造函数的异常类
- 打开CV异常错误,尽管我的代码是正确的
- 为什么没有发现我的异常
- 为什么没有捕获我来自析构函数的异常
- 存在内存访问异常,但我不确定我的代码中出了什么问题
- 为什么在我的C 代码中抛出了这个确切的异常
- Catch2 迫使我在异常中添加对 std::string 的强制转换,这会产生其他问题吗?
- 为什么 std::exception 会在 std::bad_alloc 之前捕获我的异常
- 我的数组大小调整代码在新数据上引发异常[size_t]
- 为什么我的析构函数中的异常没有触发 std::终止?
- 我应该声明我的异常的副本构造函数noexcept吗
- 如何用支持__LINE_和__FILE__的内联函数替换我的c++异常宏
- 无法捕获我的异常
- C++ 我不明白我的异常是什么()行为
- ASSERT_THROW认为我的异常属于另一种类型
- 我的异常在编译Qt Creator时没有被捕获
- 请纠正我的异常处理问题
- 我应该如何实现我的c++异常