#include #include #include using namespace std; enum ExitT {RETURN, EXIT, EXIT_, FALL, ABORT, EEXIT, EXCEPT}; void myHandler(void) { cout << "\t\t*******************" << endl; cout << "\t\tIn the exit handler" << endl; cout << "\t\t*******************" << endl; } int main(int argc, char * argv[]) { ExitT type= RETURN; int exitCode =0; bool useHandler = true; int i=1; while (i < argc) { if (!strcmp(argv[i],"--code")) { i++; if (i < argc) { exitCode = atoi(argv[i]); i++; } else { cout << "\t--code requires an integer exit code" << endl; } } else if (!strcmp(argv[i],"--exit")) { type = EXIT; i++; } else if (!strcmp(argv[i],"--exit_")) { type = EXIT_; i++; } else if (!strcmp(argv[i],"--return")) { type=RETURN; i++; } else if (!strcmp(argv[i],"--abort")) { type = ABORT; i++; } else if (!strcmp(argv[i],"--fall")) { type = FALL; i++; } else if (!strcmp(argv[i],"--Exit")) { type = EEXIT; i++; } else if (!strcmp(argv[i],"--except")) { type = EXCEPT; i++; } else if (!strcmp(argv[i],"--noHandler")) { useHandler = false; i++; } else { cout << argv[i] << " is not currently handled" << endl; i++; } } if (useHandler) { atexit(myHandler); } cout << "Remember echo $? in bash will print out the exit code. " << endl; cout << endl; switch(type) { case RETURN: cout << "Calling return(" << exitCode << ")" << endl; return (exitCode); case EXIT: cout << "Calling exit(" << exitCode << ")" << endl; exit(exitCode); case EXIT_: cout << "Calling _exit(" << exitCode << ")" << endl; _exit(exitCode); case ABORT: cout << "Calling abort()" << endl; abort(); case EEXIT: cout << "Calling _Exit()" << endl; _Exit(exitCode); case EXCEPT: cout << "Throwing a a " << exitCode << endl; throw exitCode; case FALL: default: cout << "Exiting with no call " << endl; } }