C++ newbie: error handling--basic questions about try, throw, catch?

Antst

New member
Joined
Jul 29, 2010
Messages
24
Reaction score
0
Points
1
Hello everyone:

I'm trying to understand error handling in C++.

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.

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?

(2) Is it overkill to use try, catch, throw for something this simple (I would like to improve my style)?

(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?

(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?

Thanks a lot for any help.

Here's the example I made:

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) <= fTolerance)
cout << "Result is within range.";

else
cout << "Failed.";
throw calcError;
}

catch (const int calcError)
{
cerr << "The calculation failed.\n" << endl;
}
 
First of all start with try and catch basis,
try is used for the code that can generate an error and catch is used exact below the try to catch that error/exception generated by try block

Your answers...
(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?
=>Yes the catch must be inside your function, below the try.

(2) Is it overkill to use try, catch, throw for something this simple (I would like to improve my style)?
=>No its not.

(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?
=>If there is an error, for that error you can write handler inside the catch block like print error message, program does not exit it self, you can use "exit(0);" instead to exit the program.

(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?
=>"cerr" is an object of class ostream that represents the standard error stream. You can use your own custom message using "cout" for example cout<<"The calculation failed"; Also you can use if/else inside the catch block.

Hope this helps. All the best.
 
Back
Top