2010-11-07 109 views
8

工作,我已經從cplusplus.com採取了以下代碼:set_terminate功能不是爲我

// set_terminate example 
#include <iostream> 
#include <exception> 
#include <cstdlib> 
using namespace std; 

void myterminate() { 
    cout << "terminate handler called\n"; 
    abort(); // forces abnormal termination 
} 

int main (void) { 
    set_terminate (myterminate); 
    throw 0; // unhandled exception: calls terminate handler 
    return 0; 
} 

,因爲在代碼未處理的異常,它需要調用被設置爲終止處理程序myterminate()函數應該覆蓋默認的終止處理程序。

程序崩潰但未調用myterminate()。我正在使用Visual C++ 2008 Express Edition。

代碼的問題是什麼?

回答

10

一種可能性 - 如果您在VC++調試器中運行程序,調試器會捕獲未處理的異常,並且可能無法將控制權返回給正在運行的程序以運行myterminate。嘗試在Visual C++之外運行您的程序。

+3

是。用Ctrl + F5啓動程序。 – 2010-11-07 18:09:27

+0

它是調試器中的錯誤嗎? – bjskishore123 2010-11-08 03:30:15

+2

@ bjskishore123:不,這是一個功能:) – 2010-11-08 08:08:58