The exit() and set_terminate() functions program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: How to exit and terminate in C++ program

To show: The exit() and set_terminate() usage in C++ program

 

#include <iostream>

// exit()

#include <cstdlib>

// set_terminate()

#include <exception>

 

using namespace std;

 

void Funct()

{

cout<<"Funct() was called by terminate()."<<endl;

// 0-normal exit, non zero-exit with some error

exit(0);

}

 

int main(void)

{

try

{

set_terminate(Funct);

 

// No catch handler for this exception

throw "Out of memory!";

}

catch(int)

{

cout<<"Integer exception raised."<<endl;

}

return 0;

}

 

Output example:

 

Funct() was called by terminate().

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index