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.
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;
elsereturn 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.