set_terminate

Installs your own termination routine to be called by terminate.

typedef void (*terminate_function)();

terminate_function set_terminate( terminate_function term_func );

Routine Required Header Compatibility
set_terminate <eh.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

set_terminate returns a pointer to the previous function registered by set_terminate, so that the previous function can be restored later. If no previous function has been set, the return value may be used to restore the default behavior; this value may be NULL.

Parameter

term_func

Pointer to a terminate function that you write

Remarks

The set_terminate function installs term_func as the function called by terminate. set_terminate is used with C++ exception handling and may be called at any point in your program before the exception is thrown. terminate calls abort by default. You can change this default by writing your own termination function and calling set_terminate with the name of your function as its argument. terminate calls the last function given as an argument to set_terminate. After performing any desired cleanup tasks, term_func should exit the program. If it does not exit (if it returns to its caller), abort is called.

In a multithreaded environment, terminate functions are maintained separately for each thread. Each new thread needs to install its own terminate function. Thus, each thread is in charge of its own termination handling.

The terminate_function type is defined in EH.H as a pointer to a user-defined termination function, term_func, that returns void. Your custom function term_func can take no arguments and should not return to its caller. If it does, abort is called. An exception may not be thrown from within term_func.

Note   The set_terminate function only works outside the debugger.

Example

/* TERMINAT.CPP:  
 */
#include <eh.h>
#include <process.h>
#include <iostream.h>

void term_func();

void main()
{

    int i = 10, j = 0, result;
    set_terminate( term_func );
    try
    {
        if( j == 0 )
            throw "Divide by zero!";
        else
            result = i/j;

    }
    catch( int )
    {
        cout << "Caught some integer exception.\n";
    }
    cout << "This should never print.\n";

}
void term_func()
{
    cout << "term_func() was called by terminate().\n";

    // ... cleanup tasks performed here

    // If this function does not exit, abort is called.

    exit(-1);
}


Output

term_func() was called by terminate().

Exception Handling Routines

See Also   abort, set_unexpected, terminate, unexpected