【问题标题】:Catch not catching exception捕获不捕获异常
【发布时间】:2017-01-12 11:23:24
【问题描述】:

我在使用 Catch 编写单元测试时遇到了一个问题,即我的测试因抛出异常而失败,即使我使用了REQUIRE_THROWS_AS。这是我的测试:

SECTION("Get column index for inexistent name") {
    REQUIRE_THROWS_AS(result->column_index("inexistent"), std::out_of_range);
}

这是我在控制台上遇到的异常:

$ tests/unit_tests "Test Result"
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: Name not found.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
unit_tests is a Catch v1.5.8 host application.
Run with -? for options

-------------------------------------------------------------------------------
Test Result
  Query empty table
  Get column index for inexistent name
-------------------------------------------------------------------------------
<path to file>:22
...............................................................................

<path to file>:46: FAILED:
due to a fatal error condition:
  SIGABRT - Abort (abnormal termination) signal

===============================================================================
test cases: 1 | 1 failed
assertions: 8 | 7 passed | 1 failed

如果我理解 Catch 这个异常正是我想要捕捉的,对吗?

【问题讨论】:

  • 我不知道 Catch,看起来异常是在 REQUIRE_THROWS_AS 有机会做任何事情之前引发的。你可能需要REQUIRE_THROWS_AS(result-&gt;column_index, "inexistent", std::out_of_range); 这样的东西,所以REQUIRE_THROWS_AS 有机会为你制作一个try-catch 块。
  • 您尝试过单步测试吗?正如 nwp 所说,它将证明您的异常是否在您认为抛出的地方抛出
  • 您似乎没有在指定的地方捕获异常,请尝试使用 try{}catch(){}catch(...){} 临时替换宏并进行调试。
  • REQUIRE_THROWS_AS 应该可以工作,因为one had opposite problem
  • 并且不要跨 DLL 边界使用 C++ 异常。这也可能是原因。

标签: c++ c++11 catch-unit-test


【解决方案1】:

make clean 解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    相关资源
    最近更新 更多