Share via


terminate (CRT)

呼叫 abort 或您使用 set_terminate 指定的函式。

語法

void terminate( void );

備註

terminate 函式用於 C++ 例外狀況處理,並在下列情況呼叫︰

  • 找不到擲回 C++ 例外狀況的相符 catch 處理常式。

  • 解構函式在堆疊回溯期間擲回例外狀況。

  • 堆疊在擲回例外狀況之後損毀。

terminate 預設會呼叫 abort。 您可以變更這個預設值,方法是撰寫您自己的終止函式,並使用您的函式名稱作為引數呼叫 set_terminateterminate 會呼叫指定為 set_terminate 引數的最後一個函式。 如需詳細資訊,請參閱未處理的 C++ 例外狀況

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的標頭
terminate <eh.h>

如需相容性詳細資訊,請參閱相容性

範例

// 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 );
    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);
}
term_func() was called by terminate().

另請參閱

例外狀況處理常式
abort
_set_se_translator
set_terminate
set_unexpected
unexpected