title: C++ catch error and exit the function
body: <p>I use try{} catch(){} to handle errors in a function which return a template type.</p>
  
  <pre><code> T get (int iOffset) const
      {
          try {
              checkIndex(iOffset);
          }
          catch (char const* msg) {
              std::cout &lt;&lt; msg &lt;&lt; std::endl;
          }
          int index = (m_iReadIdx + iOffset) % m_iBuffLength;
          float  a = m_ptBuff[index];
          return a;
      }
  </code></pre>
  
  <p>The function would first call <code>checkIndex</code> to check whether the input is out of range and throw an error if so. </p>
  
  <p>However, I don't want the outside <code>get</code> return any value if <code>checkIndex</code> throws an error, because the returned value may be used by other functions or printed out incorrectly. If I put a return in the catch block, I don't know what to return since it's a template. If I don't, the codes following the catch block will still get executed and therefore return a value.</p>
  
  <p>Is there any way to do that? I'm new to C++ and wondering how people usually do the error handling in this condition? THanks!</p>