google/googletest

Extended exception handling

Open

#3,433 创建于 2021年6月7日

在 GitHub 查看
 (5 评论) (3 反应) (0 负责人)C++ (38,603 star) (10,772 fork)batch import
enhancementhelp wanted

描述

Hi, I think we need to make it possible to install your own exception handler. since it is always very inconvenient to write such code (not beautiful)

TEST(ATweet, RequiresUserNameToStartWithAtSign) {
   try {
      throw my_custom_exception();
   }
   catch(const InvalidUserException& expected) {
      ASSERT_STREQ("notStartingWith@", expected.what());
   }
}

maybe I can write the code myself - I just have to decide how to implement it. 2 options come to my mind

  1. pass the exception to a custom handler function, something like this(pseudo code)
 static std::function<Result(const std::exception_ptr&)> handler = nullptr;
   if(handler !=nullptr) {
            try{
                return HandleSehExceptionsInMethodIfSupported(object, method, location);
            } catch (...) {  
                 std::exception_ptr p = std::current_exception();
                 handler(p) ;
           }
  }else{

     try {
      return HandleSehExceptionsInMethodIfSupported(object, method, location);
    } catch (const internal::GoogleTestFailureException&) {  // NOLINT
      throw;
    } catch (const std::exception& e) {  // NOLINT
      internal::ReportFailureInUnknownLocation(
          TestPartResult::kFatalFailure,
          FormatCxxExceptionMessage(e.what(), location));
    } catch (...) {  // NOLINT
     // internal::ReportFailureInUnknownLocation(
        //  TestPartResult::kFatalFailure,
         // FormatCxxExceptionMessage(NULL, location));
      
    }
    return static_cast<Result>(0);
  1. I like it much less - create and insert define

贡献者指南