C++的标准异常语句try catch只能捕获c++自身的异常。也就是throw 语句抛出的异常(也叫软异常)
而项目中很多地方出错的原因是由于空指针访问,除0,或者堆栈溢出所导致的。(通常是Win32 系统异常,可能是软异常也可能是赢异常)
这就需要有代码能捕获这些异常。
而C++的try/catch是无法捕获这种一异常的。
虽然有一个catch(...)在设置了编译属性以后可以捕获SEH,但是可以提供的有价值的信息几乎为0.
然后windows系统默认的SEH机制,通常不能用于有析构函数的代码。也就是说如果代码中使用了C++的类,存在析构函数
__try/__catch就会导致不能执行unwind而编译错误,错误信息通常如下:
error C2712: Cannot use __try in functions that require object unwinding
那有没有办法在c++代码中捕获这种异常呢?
据说使用GX链接开关可以。我没研究出来。
在网上到一篇整合了Win32结构并在C++代码中使用的文章写的非常好
http://blog.youkuaiyun.com/chenyu2202863/article/details/3260345
这里讲解一下:
一,首先需要设置一个转义函数,能够将SEH解释成c++的异常。
例如一下代码:
_set_se_translator(seh_translator);
设置一个回调函数seh_translator为转义函数。
二,转义函数,对常见的Win32异常进行了封装,将异常代码转义成特定的异常类并抛出。
// global structured exception handler translator call back function.
static void seh_translator(unsigned code, EXCEPTION_POINTERS * info)
{
switch (code)
{
case EXCEPTION_ACCESS_VIOLATION:
throw access_violation(*info);
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
throw divide_by_zero(*info);
break;
case EXCEPTION_STACK_OVERFLOW:
throw stack_overflow(*info);
default:
throw structured_exception(*info);
break;
}
}
三,这样C/C++的Try/Catch语句就可以捕获到这样的异常。
int main()
{
structured_exception::install();
//
// discriminate exception by dynamic type
//
try
{
*(int *) 0 = 0; // generate Structured Exception
}
catch (structured_exception const &exception)
{
cout << "caught " << exception.what() << endl;
}
//
// discriminate exception by static type
//
try
{
static volatile int i = 0;
i = 1 / i; // generate Structured Exception
}
catch (access_violation const &)
{
cout << "caught access violation" << endl;
}
catch (divide_by_zero const &)
{
cout << "caught divide by zero" << endl;
}
catch (structured_exception const &)
{
cout << "caught unspecified Structured Exception" << endl;
}
return 0;
}
四,项目配置
要启用SEH转换成C++ Exception的功能也就是让__set_seh_translator有效,需要打开如下编译器选项。
该函数是针对线程有效的,因此要启用该函数需要对每个 线程都进行一次设置。
该函数是针对模块有效的(也就是native code)。如果你的项目有多个DLL,只进行一次设置是无效的。必须将相关的代码编译成native code。(等待测试)
一个测试代码:
Exception.h
#ifndef XAP_WIN32SEH_H
#define XAP_WIN32SEH_H
#include <ut_export.h>
#include <windows.h>
#include <exception>
//
class ABI_EXPORT structured_exception : public std::exception
{
public:
structured_exception(EXCEPTION_POINTERS const &) throw();
static void install() throw();
virtual const char* what() const throw();
const void * address() const throw();
unsigned getExceptionCode() const throw();
private:
const void *m_ExceptionAddress;
unsigned m_Exceptioncode;
};
//
// handle for EXCEPTION_ACCESS_VIOLATION
class ABI_EXPORT access_violation : public structured_exception
{
public:
access_violation(EXCEPTION_POINTERS const&) throw();
virtual const char* what() const throw();
};
// handle for EXCEPTION_INT_DIVIDE_BY_ZERO
class ABI_EXPORT divide_by_zero : public structured_exception
{
public:
divide_by_zero(EXCEPTION_POINTERS const &) throw();
virtual const char* what() const throw();
};
// handle for EXCEPTION_STACK_OVERFLOW
class ABI_EXPORT stack_overflow : public structured_exception
{
public:
stack_overflow(EXCEPTION_POINTERS const &) throw();
virtual const char* what() const throw();
};
#endif
Exception.cpp
#include "xap_Win32SEH.h"
#include <eh.h> // for _set_se_translator
// global structured exception handler translator call back function.
static void seh_translator(unsigned code, EXCEPTION_POINTERS * info)
{
switch (code)
{
case EXCEPTION_ACCESS_VIOLATION:
throw access_violation(*info);
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
throw divide_by_zero(*info);
break;
case EXCEPTION_STACK_OVERFLOW:
throw stack_overflow(*info);
default:
// we only throw the structured exception which is known.
//throw structured_exception(*info);
break;
}
}
structured_exception::structured_exception(EXCEPTION_POINTERS const & info) throw()
{
EXCEPTION_RECORD const &exception = *(info.ExceptionRecord);
m_ExceptionAddress = exception.ExceptionAddress;
m_Exceptioncode = exception.ExceptionCode;
}
// install the global structured exception handler translator.
void structured_exception::install() throw()
{
_set_se_translator(seh_translator);
}
// return exception type string
const char * structured_exception::what() const throw()
{
return "unspecified structured exception";
}
// return exception address.
const void * structured_exception::address() const throw()
{
return m_ExceptionAddress;
}
// return exception code.
unsigned structured_exception::getExceptionCode() const throw()
{
return m_Exceptioncode;
}
//
// Implementation for Win32 SEH class
//
// access_violation:
//
access_violation::access_violation(EXCEPTION_POINTERS const& info) throw()
: structured_exception(info)
{
}
const char* access_violation::what() const throw()
{
return "access violation";
}
//
// divide_by_zero:
//
divide_by_zero::divide_by_zero(EXCEPTION_POINTERS const & info) throw()
: structured_exception(info)
{
}
const char* divide_by_zero::what() const throw()
{
return "divide by zero";
}
//
// stack_overflow
//
stack_overflow::stack_overflow(EXCEPTION_POINTERS const & info) throw()
: structured_exception(info)
{
}
const char* stack_overflow::what() const throw()
{
return "stack overflow";
}