【问题标题】:Testing exceptions with BOOST使用 BOOST 测试异常
【发布时间】:2011-12-23 06:46:28
【问题描述】:

我正在使用 boost 测试框架 1.47,但在测试我的异常时遇到了困难

这是我的异常类

class VideoCaptureException : public std::exception
{

    std::string m_Description;
public:
    VideoCaptureException(const char* description)
    {
        m_Description = std::string(description);
    }
    VideoCaptureException(const std::string& description)
    {
        m_Description = description;
    }
    virtual ~VideoCaptureException() throw() {}
    virtual const char* what() const throw()
    {
        return m_Description.c_str();
    }
}

我正在尝试测试仅引发此异常的代码

BOOST_CHECK_THROW( source.StopCapture(), VideoCaptureException )

由于某种原因它不起作用。

unknown location(0): fatal error in "testVideoCaptureSource": unknown type
testVideoCaptureSource.cpp(28): last checkpoint

我做错了什么?

【问题讨论】:

  • 类定义后缺少分号? :)
  • source.StopCapture() 周围添加额外的括号有帮助吗?
  • @FredOverflow :原始版本有分号 =)。代码编译并“正确”运行。我现在正在尝试测试它。此外,括号没有帮助
  • 这是单个 EXE 还是 DLL 中的一些代码? (假设 Windows 是您的平台。)
  • 我在 Linux 上,我一次性构建了所有内容,因此除了 boost 之外没有其他链接

标签: c++ exception testing boost


【解决方案1】:

在自己遇到这个错误后,我发现它是一个愚蠢但容易犯的错误:

throw new VideoCaptureException( "uh-oh" );

将失败并显示该错误消息,而:

throw VideoCaptureException( "uh-oh" );

会成功的。


new 变体导致捕获指向异常的指针,而不是异常本身。 boost 库不知道如何处理它,所以它只是说“未知类型”。

如果库能正确解释这种情况会很好,但希望任何其他点击“致命错误:未知类型”的人都能找到此页面并查看如何修复它!

【讨论】:

  • 我想报告我找到了这个页面并且能够修复它:) 花费了我生命中至少一个小时...
  • 刚刚注意到这个答案。也许你会在 4 年后回复这条评论
  • 值得注意的是,在仍然存在此错误的 Boost 版本中,BOOST_CHECK_EXCEPTION 即使将指针作为异常也可以正常工作。当然,要解决这个问题,1. 更新 Boost,2. 不要将异常作为指针抛出(但在某些尴尬的情况下,任何一个选项都可能不可用......)
猜你喜欢
  • 2016-06-22
  • 2011-02-24
  • 1970-01-01
  • 2015-10-17
  • 2017-03-06
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多