Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Calls abort or a function you specify using set_terminate.

void terminate( void );


The terminate function is used with C++ exception handling and is called in the following cases:

  • A matching catch handler cannot be found for a thrown C++ exception.
  • An exception is thrown by a destructor function during stack unwind.
  • The stack is corrupted after throwing an exception.

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. For more information, see Unhandled C++ Exceptions.


Routine Required header Compatibility
terminate <eh.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_terminate.cpp
// compile with: /EHsc
#include <eh.h>
#include <process.h>
#include <iostream>
using namespace std;

void term_func();

int main()
    int i = 10, j = 0, result;
    set_terminate( term_func );
        if( j == 0 )
            throw "Divide by zero!";
            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.



term_func() was called by terminate().

See Also

Exception Handling Routines | abort | _set_se_translator | set_terminate | set_unexpected | unexpected | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft