title: C++ newbie question--basic error handling using try, throw, catch
body: <p>I'm trying to understand error handling in C++.</p>
  
  <p>I have read that using try, throw, catch is better style and less complicated than using if statements with return values. But I'm not sure I really understand how try, throw, catch works. I made a simple example below and it would be great to get feedback about any problems or bad style. My goal is to make a function out of the example that checks the results of another calculation.</p>
  
  <p>Here are questions I have about try, throw, catch:
  (1) Should the catch statement be included in my function? Or should it be somewhere else, like in main() or in the function where the initial calculation is done?</p>
  
  <p>(2) Is it overkill to use try, catch, throw for something this simple (I would like to improve my style)?</p>
  
  <p>(3) If there is an error, I would like to terminate the program. How would I do that? Or does "catch" mean that that is done automatically?</p>
  
  <p>(4) I don't understand the use of cerr. Why not just use cout? Have I used cerr correctly here? Should I also have used it in the if/else statements?</p>
  
  <p>Thanks a lot for any help.</p>
  
  <p>Here's the example I made:</p>
  
  <pre><code>double calculated = 10.2; // from previous calculation
  double tolerance = 0.3; // I can set this in this function
  double valueWanted = 10.0; // from previous calculation
  
  const int calcError = 5; // I picked this number randomly to use for indicating an error
  
  try
  {
     if (fabs(fTargetValue - fCalculated) &lt;= fTolerance)
       cout &lt;&lt; "Result is within range.";
  
     else
       cout &lt;&lt; "Failed.";
       throw calcError;
  }
  
  catch (const int calcError)
  {
     cerr &lt;&lt; "The calculation failed.\n" &lt;&lt; endl;
  }
  </code></pre>