VC ++异常处理-应用程序仍然崩溃 [英] VC++ exception handling - app crashes anyway
问题描述
以下是我在学习C ++中的异常处理过程中(使用Visual Studio 2010编译器)编写的代码段。
Following is the code snippet I have written in the process of learning exception handling in C++(using visual studio 2010 compiler).
#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]。
但是当我运行该应用程序时,它仍然仍然崩溃!带有以下信息:
Enable C++ exception is set to Yes[/EHsc]. But when I run the application,it is still crashing anyway ! with following information:
问题签名:
问题事件名称:APPCRASH
应用程序名称:DataTypeConversions.exe
应用程序版本:0.0。 0.0
应用程序时间戳:4ffd8c3d
故障模块名称:DataTypeConversions.exe
故障模块版本:0.0.0.0
故障模块时间戳:4ffd8c3d
异常代码:c0000005
异常偏移量:00001051
Problem signature: Problem Event Name: APPCRASH Application Name: DataTypeConversions.exe Application Version: 0.0.0.0 Application Timestamp: 4ffd8c3d Fault Module Name: DataTypeConversions.exe Fault Module Version: 0.0.0.0 Fault Module Timestamp: 4ffd8c3d Exception Code: c0000005 Exception Offset: 00001051
为什么控件不捕获块?!
Why is not control coming to catch block?!
推荐答案
访问冲突和所有其他类型的硬件异常在Windows中使用称为 C结构化异常处理(SEH)的机制进行处理。与基于Posix的系统中通常的signal()/ sigaction()机制相比,此方法最初旨在为C程序提供一种更加结构化的方式来处理异常。
Access Violations and all other kinds of hardware exceptions are handled in Windows using a mechanism called "C Structured Exception Handling (SEH)". This was originally designed to give C programs a more "structured" way to handle exceptions than the usual signal()/sigaction() mechanism in Posix based systems.
SEH异常 可以通过设置在SEH堆栈展开之前调用的转换函数集成到C ++异常系统中。新的翻译器功能仅引发C ++异常,并且可以证明C ++可以捕获错误!
SEH exceptions can be integrated into the C++ Exception system, by setting a translator function which is called before SEH stack unwinding takes place. The new translator function simply throws a C++ exception and, presto, C++ can catch the error!
有关所有详细信息,请参阅MSDN上的此文档:
See this Document from the MSDN for all the details:
http: //msdn.microsoft.com/de-de/library/5z4bw5h5(v=vs.80).aspx
这是一个工作示例:
#include <windows.h>
#include <iostream>
#include <eh.h>
// You need to enable the /EHa excpetion model to make this work.
// Go to
// Project|Properties|C/C++|Code Generation|Enable C++ Exceptions
// and select "Yes with SEH Exceptions (/EHa)"
void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
{
// printf( "In trans_func.\n" );
throw "Hardware exception encountered";
}
int main()
{
_set_se_translator(trans_func);
try
{
int *p = NULL;
*p = 0;//expecting access violation exception
}
catch(const char *s)
{
std::cout << "caught an exception:" << s << "\n";
}
return 0;
}
这篇关于VC ++异常处理-应用程序仍然崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!