捕获未捕获异常

Catch not catching exception

本文关键字:捕获异常      更新时间:2023-10-16

我正在使用 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 此异常正是我想要捕获的,对吗?

make clean解决了这个问题。