Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法在google测试中检查异常类型_C++_Exception_Exception Handling_Googletest - Fatal编程技术网

C++ 无法在google测试中检查异常类型

C++ 无法在google测试中检查异常类型,c++,exception,exception-handling,googletest,C++,Exception,Exception Handling,Googletest,我无法检查gtests中代码引发的异常。下面是运行测试的测试套件的一个片段: EXPECT_THROW({ try{ // Insert a tuple with more target columns than values rows_changed = 0; query = "INSERT INTO test8(num1, num3) VALUES(3);"; txn = txn

我无法检查gtests中代码引发的异常。下面是运行测试的测试套件的一个片段:

EXPECT_THROW({
        try{
            // Insert a tuple with more target columns than values
            rows_changed = 0;
            query = "INSERT INTO test8(num1, num3) VALUES(3);";

            txn = txn_manager.BeginTransaction();
            plan = TestingSQLUtil::GeneratePlanWithOptimizer(optimizer, query, txn);
            EXPECT_EQ(plan->GetPlanNodeType(), PlanNodeType::INSERT);
            txn_manager.CommitTransaction(txn);
            TestingSQLUtil::ExecuteSQLQueryWithOptimizer(
                optimizer, query, result, tuple_descriptor, rows_changed, error_message);
            }
        catch (CatalogException &ex){
            EXPECT_STREQ("ERROR:  INSERT has more target columns than expressions", ex.what());
            }
  }, CatalogException);
我很确定CatalogException被抛出。我甚至尝试通过将抛出的异常输出到cerr来获取其详细信息,结果显示异常类型为Catalog

这不是一个重复的问题,我搜索了答案,所以我没有在我的代码中使用新的抛出错误。下面是这样做的代码片段:

if (columns->size() < tup_size)
      throw CatalogException(
          "ERROR:  INSERT has more expressions than target columns");
EXPECT_THROW的思想是,宏捕获异常。如果您自己捕获了异常,gmock现在不会对抛出的异常进行任何处理

我建议将语句写入EXPECT\u抛出,这实际上触发了异常。其他的都可以在之前写

例如:

 TEST(testcase, testname) 
 {
  //arrange everything:
  //...
  //act + assert:
  EXPECT_THROW(TestingSQLUtil::ExecuteSQLQueryWithOptimizer( optimizer, query, result,
   tuple_descriptor, rows_changed, error_message)
   ,CatalogException);
}
我假设TestingSQLUtil::ExecuteSQLQueryWithOptimizer触发了抛出的异常

补充: 我试图重建您的异常层次结构。这个例子对我很有用。测试通过,这意味着抛出异常

enum class ExceptionType
{
    CATALOG
};

class Exception {
public:
    Exception(ExceptionType type, std::string msg) {}
};

class CatalogException : public Exception {
    CatalogException() = delete;

public:
    CatalogException(std::string msg)  : Exception(ExceptionType::CATALOG, msg) {}
};

void testThrow() {
    throw CatalogException( "ERROR:  INSERT has more expressions than target columns");
}

TEST(a,b) {
    EXPECT_THROW( testThrow(), CatalogException);
}

catch捕获异常,并且不会在catch块中再次抛出,那么异常来自何处?@TobiasWollgam它来自我提到的最后一个代码块,它位于不同的文件中。该文件中的代码在try块中执行。但是catch块“使用”了异常。之后异常就消失了。@TobiasWollgam但是测试套件应该说没有抛出任何东西,但是它说抛出了另一种类型的异常。啊,好的。那么异常的类型是Catalog?
enum class ExceptionType
{
    CATALOG
};

class Exception {
public:
    Exception(ExceptionType type, std::string msg) {}
};

class CatalogException : public Exception {
    CatalogException() = delete;

public:
    CatalogException(std::string msg)  : Exception(ExceptionType::CATALOG, msg) {}
};

void testThrow() {
    throw CatalogException( "ERROR:  INSERT has more expressions than target columns");
}

TEST(a,b) {
    EXPECT_THROW( testThrow(), CatalogException);
}