分类: C/C++
2010-11-28 14:29:30
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
might abort
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 the atexit
and on_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 the std::set_terminate
function, which by default simply calls abort
.