Exemple #1
0
int main()
{
    try
    {
        throwingFunction();
    }
    catch(std::runtime_error)
    {
        std::cout << "exception caught!\n";
    }
    std::cout << "if you can't see a destructor of HeavyClass we have a memory leak!\n";

}
      EXPECT_EQ(0, folly::uncaught_exceptions());
      Validator validatorRethrow(1, "catch_rethrow");
      throw;
    }
  } catch (const std::runtime_error& e) {
    EXPECT_EQ(0, folly::uncaught_exceptions());
  }
}

[[noreturn]] void throwingFunction() {
  Validator validator(1, "one_uncaught_exception_in_function");
  throw std::runtime_error("exception");
}

TEST(UncaughtExceptions, one_uncaught_exception_in_function) {
  EXPECT_THROW({ throwingFunction(); }, std::runtime_error);
}

/*
 * Test helper class. Generates N wrapped classes/objects.
 * The destructor N of the most outer class creates the N-1
 * object, and N - 1 object destructor creating the N-2 object,
 * and so on. Each destructor throws an exception after creating
 * the inner object on the stack, thus the number of exceptions
 * accumulates while the stack is unwinding. It's used to test
 * the folly::uncaught_exceptions() with value >= 2.
 */
template <size_t N, size_t I = N>
struct ThrowInDestructor {
  using InnerThrowInDestructor = ThrowInDestructor<N, I - 1>;
  ThrowInDestructor() {}