Ela782
Ela782

Reputation: 5181

When should I return EXIT_SUCCESS and when EXIT_FAILURE? What does successful termination mean?

In the main-function of a C++ application, when should I return EXIT_SUCCESS and when EXIT_FAILURE? What does successful termination mean? To give a few examples:

For many of these examples, the program runs as intended, it has correct behavior, so it is a case for returning EXIT_SUCCESS. However, the program did not complete its main operation, its main purpose correctly because of an error on the way, so that speaks for EXIT_FAILURE.

I'm also thinking about that the user might run the program from a bash-script (or from Matlab or whatever) and might be interested in its return value to find out if it completed its operation. This would mean we'd have to return EXIT_FAILURE in all above cases.

What's the best practice here?

Upvotes: 1

Views: 2722

Answers (4)

Peixu Zhu
Peixu Zhu

Reputation: 2151

From the C++ & C specification, the value of zero or EXIT_SUCCESS is proposed to tell the host environment (like a bash etc.) that the program successfully terminated, and EXIT_FAILURE is proposed to tell the host environment that the program unsuccessfully terminated, you can also set the return value or exit status to other integer values.

Thus, if you wanna to tell the host environment that your program runs okay, please return EXIT_SUCCESS, or return EXIT_FAILURE to tell the host environment that the program fails to run, and return other values for program specific status.

Upvotes: 0

sehe
sehe

Reputation: 392954

It's up to you. There are conventions for scripting, but in essence, exit codes are just an int, and you assign it meanings.

See e.g. https://superuser.com/questions/280425/getting-robocopy-to-return-a-proper-exit-code

MSDN says:

enter image description here

Devise your own exit code scheme, and take the good lessons shown by RoboCopy to heart

Upvotes: 0

Pekka
Pekka

Reputation: 3654

This depends on your application. Return failure in cases where a script could take a different action. If you cannot conceive anyone wanting to even log faults, then it does not really matter.

Upvotes: 0

slugonamission
slugonamission

Reputation: 9642

The return values of these programs are so that they can be executed in a script (or a sequence), and the error condition can be caught by the script (since the script won't be monitoring stdout/stderr for textual output), and action can be taken to either carry on, abort or attempt to rectify the situation.

For this reason, anything which is abnormal termination is a failure condition. If you haven't passed the correct arguments, so the program emits the help text, it's terminating abnormally, since the program has hit an error condition (it doesn't know what to do). If the config is incorrect, it's an error and therefore an abnormal termination.

Basically, if the program hasn't fulfilled its actual purpose, it hasn't been successful.

Upvotes: 3

Related Questions