EXIT_FAILURE

While studying the primer,I found the following example:
#include <cstdlib>
int main()
{
if (some_failure)
return EXIT_FAILURE;
else
return EXIT_SUCCESS;
}


The question is: Which way can I use this code? I tried, for example, with division by zero, but this is a runtime error, a different thing.
Thank you.
I think you should use it like this:

1
2
3
4
5
6
7
8
9
10
11
bool some_failure = false;

int main()
{
     // do your calculations here and set some_failure true if you get any error

     if (some_failure)
          return EXIT_FAILURE;
     else
          return EXIT_SUCCESS;
}
The primer is just giving you a suggestion of how to handle application-level errors.

Certain errors the operating system doesn't like -- division by zero, floating point exceptions, segmentation faults, etc. Those the OS handles for you by terminating your application.

The code above is useful for handling errors you can detect yourself. For example, the user tells you to read a certain file but it doesn't exist.
Which version of primer are you using. I had been uning the 4th Edition and that one has some really nice code in it.
OK, it is clear now. Thank you.
Topic archived. No new replies allowed.