abort, terminate or exit?
Solution 1:
abort indicates "abnormal" end to the program, and raises the the POSIX signal SIGABRT, which means that any handler that you have registered for that signal will be invoked, although the program will still terminate afterwords in either case. Usually you would use
abort
in a C program to exit from an unexpected error case where the error is likely to be a bug in the program, rather than something like bad input or a network failure. For example, you mightabort
if a data structure was found to have a NULL pointer in it when that should logically never happen.exit indicates a "normal" end to the program, although this may still indicate a failure (but not a bug). In other words, you might
exit
with an error code if the user gave input that could not be parsed, or a file could not be read. An exit code of 0 indicates success.exit
also optionally calls handlers before it ends the program. These are registered with theatexit
andon_exit
functions.std::terminate is what is automatically called in a C++ program when there is an unhandled exception. This is essentially the C++ equivalent to
abort
, assuming that you are reporting all your exceptional errors by means of throwing exceptions. This calls a handler that is set by thestd::set_terminate
function, which by default simply callsabort
.
In C++, you usually want to avoid calling abort
or exit
on error, since you're better off throwing an exception and letting code further up the call stack decide whether or not ending the program is appropriate. Whether or not you use exit
for success is a matter of circumstance - whether or not it makes sense to end the program somewhere other than the return statement in main
.
std::terminate
should be considered a last-ditch error reporting tool, even in C++. The problem with std::terminate
is that the terminate handler does not have access to the exception that went unhandled, so there's no way to tell what it was. You're usually much better off wrapping the entirety of main in a try { } catch (std::exception& ex) { }
block. At least then you can report more information about exceptions that derived from std::exception
(although of course exceptions that do not derive from std::exception
would still end up unhandled).
Wrapping the body of main
in try { } catch(...) { }
isn't much better than setting a terminate handler, because again you have no access to the exception in question. Edit: Per Neil Butterworth's answer, there is a benefit in that the stack is unwound in this case, which is (somewhat surprisingly) not true for an unhandled exception.
Solution 2:
std::abort and std::exit (and more: std::_Exit, std::quick_exit) are just lower level functions. You use them to tell the program what you want it to do exactly: what destructors (and if) to call, what other clean-up functions to call, what value to return, etc.
std::terminate is a higher level abstraction: it is called (by either run-time or you) to indicate that an error in the program occurred and that for some reason it is not possible to handle by throwing an exception. The necessity for that typically occurs when error occurs in the exception mechanism itself, but you can use it any time when you do not want your program to continue beyond the given error. I compiled the full list of situations when std::terminate is called in my post. It is not specified what std::terminate does, because you are in control of it. You can configure the behavior by registering any functions. The limitations you have are that the function cannot return back to the error site and it cannot exit via an exception, but technically you can even start your message pump inside. For the list of useful things that you can do inside, see my other post.
In particular, note that std::terminate is considered an exception handler in contexts where std::terminate is called due to a thrown exception that could not be handled, and you can check what the exception was and inspect it by using C++11 using std::rethrow_exception and std::current_exception. It is all in my post.
Solution 3:
quick_exit() !
If your program is multi-threaded, then calling exit()
will most likely result in a crash because global/static std::thread
objects will be attempted to destruct without exiting their threads.
If you want to return an error code and exit the program (more or less) normally, call quick_exit()
in multi-threaded programs.
For abnormal termination (without a possibility for you to specify the error code), abort()
or std::terminate()
can be called.
Note: quick_exit() has not been supported by MSVC++ until version 2015 .
Solution 4:
-
terminate() is automatically called when an exception occurs that cannot be handled. By default, terminate() calls abort(). You can set a custom handle with set_terminate() function.
abort() sends the SIGABRT signal.
exit() is not necessarily a bad thing. It successfully exits the application, and calls atexit() functions in LIFO order. I don't normally see this in C++ applications, however, I do see it in many unix based applications where it sends an exit code at the end. Usually a exit(0) indicates a successful run of the application.