0

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;
}
malajedala
  • 867
  • 2
  • 8
  • 22
  • 2
    ***maybe someone has a good advice for me*** Avoid tutorials. Instead get a good `c++` book. You will not learn `c++` using tutorials. Here is a recommended list of `c++` books: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – drescherjm Oct 05 '18 at 16:23
  • 3
    You cannot `return 0;` in a constructor, it doesn't return any type. Use just `return;` instead, or even better throw an exception. – πάντα ῥεῖ Oct 05 '18 at 16:23
  • Throw an exception. If the program does not catch it, the program will terminate – Remy Lebeau Oct 05 '18 at 16:49
  • This is more of a style question, personally I would just accept all possible values/outcomes in the constructor (even failure to open a file) and then handle the situation correctly in all member functions. It's more work but much cleaner and robust code. – Hugo Maxwell Oct 05 '18 at 16:50
  • to Hugo Maxwell: how exactly would you handle the situation in the member functions. You would check in a function, if the coordinates of the object are in the allowed limit set? – malajedala Oct 05 '18 at 17:41

1 Answers1

5

When a constructor fails throw an exception.

Because:

  • There is no point in having an object that has not initialized correctly.
  • It is impossible to accidentally ignore an exception (unlike error codes or log messages).
  • You either have a correctly initialized object, or you don't have an object at all. Simple and robust.
  • The destructor doesn't need to handle a partially initialized object destruction, which may not be possible at all, since uninitialized members may have indeterminate values. When a constructor throws an exception the destructor is not called (although the destructors of already initialized members and base classes are called in the reverse order).
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • @MaxLanghof Well, even if you catch that exception, the object did not construct and never came into existence, so you cannot access it by any means. – Maxim Egorushkin Oct 05 '18 at 16:39