Proper way to exit after encountering an error?

if(usleep(timeToSleep) == -1) {
perror("Failed to sleep.");

Lets say timeToSleep() returns -1 which signifies an error.
What kind of exit should I use in this case?
I know there are a few different exit functions,
is exit(1) ok to use in this case? What would you recommend?
closed account (Dy7SLyTq)
use perror as the arg for exit
On error you have to exit with any value but 0.

Mostly applications return 1 on general errors. But it may depend on your decision to exit with some other value to distinguish different error conditions in your calling application. This may sometimes be useful in shell scripts.

usleep() only returns -1 on being interrupted. Global variable errno then may take the value EINTR. Depending on your program this may be not indicate an error but f.e. indicate some expected signal.
F.e. if your process forked off a child. Then after exit of child your parent process may receive signal SIGCHLD which indicates death of your child. You may want to handle this signal by catching the child (wait(2)). This may not indicate an error condition but a well defined behavior of your program.
Topic archived. No new replies allowed.