google/googletest

Extended exception handling

Open

#3,433 opened on Jun 7, 2021

View on GitHub
 (5 comments) (3 reactions) (0 assignees)C++ (38,603 stars) (10,772 forks)batch import
enhancementhelp wanted

Description

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

Contributor guide