I am trying to learn C++ (currently working with C), and I am spending time searching for the internet for good tutorials (maybe someone has a good advice for me).
But I could not find information about the standard way of error handling.
So lets say I have a simple class like this (my example.h file):
const int MAX = 1000;
class Example {
public:
Example(int x);
private:
int x_val;
}
So what I simply want to do is to check, when a object of the class Example is created, if the given coordinate is allowed, and if it is higher than the allowed, abort the program?
So In the .cpp file:
Example::Example(int x){
/*So is it common, to do something like this:*/
if (x >= MAX){
std::cerr << "Error while generating example object" << std::endl;
return 0;
}
/*or is this more convenient:*/
throw std::invalid_argument( "received to high value" );
x_val = x;
}