What is the difference between exit() and abort()?


In C and C++, what is the difference between exit() and abort()? I am trying to end my program after an error (not an exception).

5/5/2010 9:40:35 AM

abort() exits your program without calling functions registered using atexit() first, and without calling objects' destructors first. exit() does both before exiting your program. It does not call destructors for automatic objects though. So

A a;
void test() { 
    static A b;
    A c;

Will destruct a and b properly, but will not call destructors of c. abort() wouldn't call destructors of neither objects. As this is unfortunate, the C++ Standard describes an alternative mechanism which ensures properly termination:

Objects with automatic storage duration are all destroyed in a program whose function main() contains no automatic objects and executes the call to exit(). Control can be transferred directly to such a main() by throwing an exception that is caught in main().

struct exit_exception { 
   int c; 
   exit_exception(int c):c(c) { } 

int main() {
    try {
        // put all code in here
    } catch(exit_exception& e) {

Instead of calling exit(), arrange that code throw exit_exception(exit_code); instead.

6/7/2013 2:43:01 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow