简体   繁体   中英

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:

  • The user forgets some required command-line arguments, we catch it, print the help and exit (I would return EXIT_SUCCESS here)

  • We read a config-file, there's some syntax error in it or a file given in it can't be found, so our read-config function throws, we catch it in main and exit (I'm uncertain what to return here)

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?

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.

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.

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 eg https://superuser.com/questions/280425/getting-robocopy-to-return-a-proper-exit-code

MSDN says:

在此输入图像描述

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM