以下是我在学习C++异常处理过程中写的代码片段(使用Visual Studio 2010编译器)。
#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++ 异常设置为 Yes[/EHsc]。但是当我运行应用程序时,它仍然崩溃!带有以下信息:
问题签名:问题事件名称:APPCRASH 应用程序名称:DataTypeConversions.exe 应用程序版本:0.0.0.0 应用程序时间戳:4ffd8c3d 故障模块名称:DataTypeConversions.exe 故障模块版本:0.0.0.0 故障模块时间戳:4ffd8c3d 异常代码:c0000005 异常偏移量: 00001051
为什么不控制来抓挡?!