以下是我在C++中學習異常處理(使用visual studio 2010編譯器)的過程中編寫的代碼片段。VC++異常處理 - 應用程序崩潰無論如何
#include "stdafx.h"
#include <iostream>
using namespace std;
void multi_double() {
double first,second,product;
try{
cout << "Enter the first number\n";
cin >> first;
cout << "Enter the second number\n";
cin >> second;
product = first * second;
cout << product;
}
catch(...){cout << "Got an exceptional behaviour";}
}
void stud_age(){
int age;
try{
cout << "Enter student's age\n";
cin >> age;
if(age < 0)
throw;
cout << endl <<age;
}
catch(...) {
cout << "Caught here\n";
}
}
class Model{
public:
Model(){cout << "ctor\n";}
~Model(){cout << "dtor\n";}
};
int _tmain(int argc, _TCHAR* argv[]) {
//multi_double();
//stud_age();
int a;
try{
Model obj;
int *p = NULL;
*p = 0;//expecting access violation exception
}
catch(...){
cout << "caught an exception\n";
}
return 0;
}
啓用C++異常設置爲是[/ EHsc]。 但是當我運行該應用程序時,它仍然崩潰!具有以下信息:
問題簽名: 問題事件名稱:APPCRASH 應用名稱:DataTypeConversions.exe 應用程序版本:0.0.0.0 應用程序時間戳:4ffd8c3d 故障模塊名稱:DataTypeConversions.exe 故障模塊版本:0.0 .0.0 故障模塊時間戳:4ffd8c3d 異常代碼:0000005 異常偏移量:00001051
爲什麼不控制來catch塊?
您需要使用/ EHA編譯有趕上(... )也捕獲處理器異常。這是一個非常糟糕的想法。 – 2012-07-11 15:30:40