How can I abstract out a repeating try catch pattern in C++

前端 未结 7 1196
眼角桃花
眼角桃花 2020-12-15 04:28

I have a pattern that repeats for several member functions that looks like this:

int myClass::abstract_one(int sig1)
{
  try {
    return _original->abstr         


        
7条回答
  •  渐次进展
    2020-12-15 05:12

    As a variant on Alexander Gessler's solution you can omit some of the braces that make this implementation a little long. It does exactly the same thing, just with a little less { } verbage.

    void handle() try 
    { 
      throw;
    }
      catch (std::exception& err) 
    {
      handleException(err);
    }
      catch (MyException& err)
    {
      handleMyException(err);
    }
      catch (...)
    {
      handleException();
    }
    
    
    
    int myClass::abstract_one(int sig1) try 
    {
      return _original->abstract_one(sig1);
    }
      catch (...)
    {
      handle();
      return -1;
    }
    

提交回复
热议问题