Using exit()
is OK
Two major aspects of code design that have not yet been mentioned are 'threading' and 'libraries'.
In a single-threaded program, in the code you're writing to implement that program, using exit()
is fine. My programs use it routinely when something has gone wrong and the code isn't going to recover.
But…
However, calling exit()
is a unilateral action that can't be undone. That's why both 'threading' and 'libraries' require careful thought.
Threaded programs
If a program is multi-threaded, then using exit()
is a dramatic action which terminates all the threads. It will probably be inappropriate to exit the entire program. It may be appropriate to exit the thread, reporting an error. If you're cognizant of the design of the program, then maybe that unilateral exit is permissible, but in general, it will not be acceptable.
Library code
And that 'cognizant of the design of the program' clause applies to code in libraries, too. It is very seldom correct for a general purpose library function to call exit()
. You'd be justifiably upset if one of the standard C library functions failed to return just because of an error. (Obviously, functions like exit()
, _Exit()
, quick_exit()
, abort()
are intended not to return; that's different.) The functions in the C library therefore either "can't fail" or return an error indication somehow. If you're writing code to go into a general purpose library, you need to consider the error handling strategy for your code carefully. It should fit in with the error handling strategies of the programs with which it is intended to be used, or the error handling may be made configurable.
I have a series of library functions (in a package with header "stderr.h"
, a name which treads on thin ice) that are intended to exit as they're used for error reporting. Those functions exit by design. There are a related series of functions in the same package that report errors and do not exit. The exiting functions are implemented in terms of the non-exiting functions, of course, but that's an internal implementation detail.
I have many other library functions, and a good many of them rely on the "stderr.h"
code for error reporting. That's a design decision I made and is one that I'm OK with. But when the errors are reported with the functions that exit, it limits the general usefulness the library code. If the code calls the error reporting functions that do not exit, then the main code paths in the function have to deal with error returns sanely — detect them and relay an error indication to the calling code.
The code for my error reporting package is available in my SOQ (Stack Overflow Questions) repository on GitHub as files stderr.c
and stderr.h
in the src/libsoq sub-directory.
SIGTERM
signal on POSIX & Linux.... Well behaved servers are expected to handle it nicely. And you should avoid usingSIGKILL
(i.e. trykill -TERM
thenkill -QUIT
and only later thenkill -KILL
as a sysadmin)exit()
function is not germane (and the high vote count is surprising — it isn't a good question). Why should I not use exit() function in C? would be a good candidate if it had answers of the calibre of this question. It doesn't; a reverse close of that as a duplicate of this is appropriate — and I've done it.