Handling common parts of exceptions together

前端 未结 3 562
心在旅途
心在旅途 2021-01-18 19:47

I currently have some code I am trying to refactor. A large set of exceptions has some common code for all exceptions as well as some specific code which needs to be handled

3条回答
  •  余生分开走
    2021-01-18 20:30

    I would really just move the common code to a function.

    Here is another idea:

    try {
      /* Stuff that may fail */
    } catch( const std::exception & e) {
      /* do common part here */  
      if (const exception1 * e1 = dynamic_cast(&e) {
        /* do stuff for exception1 here */
      } else if (const exception2 * e2 = dynamic_cast(&e) {
        /* do stuff for exception2 here */
      } else
        throw;
    }
    

提交回复
热议问题