两者区别:
C++异常与SEH区别可参考《Windows核心编程第五版》第25章 25.2节(727页)
下面取自原文:
注意:
- C++异常和SEH异常不是同一个东西!!!
- C++使用try,catch作为关键字进行捕获异常(C++标准自己定义的异常)
- SEH是Windows操作系统自身使用并提供的一种捕获系统异常的方式,使用__try、__except、__finally、__leave作为关键字进行捕获异常(windows系统定义的异常)
- 无法使用C++异常处理捕获SEH异常,同样SEH异常也无法捕获C++异常
- 在windows系统下,C++异常处理内部也是封装的SEH进行处理
步骤如下:
- 修改visual studio项目配置,启用C++异常修改为:
/EHa
- 在每个线程起始处,调用
_set_se_translator
函数来设置SEH异常转换为C++异常的转换函数
int _tmain(int argc, tchar* argv[], tchar* envp[])
{
_set_se_translator(seh_excpetion::TranslateSEHtoCE);
try
{
char* pp = NULL;
*pp = 'a';
}
catch (const seh_excpetion& error)
{
printf("SEHcode: [%08x] %s", error.code(), error.what());
}
return 0;
}
执行结果:虽然对非法地址进行赋值,但是并不会发生崩溃
附seh_excpetion类代码:seh_exception.hpp
/**
* @Description: a class for convert SEH exception to c++ exception
* @Author: shang_cm
* @Date: 2020-06-01 16:09:00
* @LastEditTime: 2010-06-01 16:09:00
* @LastEditors: shang_cm
*/
#pragma once
#include <windows.h>
#include <exception>
class seh_excpetion : std::exception
{
typedef ULONG (WINAPI *fpRtlNtStatusToDosError)(NTSTATUS Status);
public:
seh_excpetion(unsigned int nExceptionCode, _EXCEPTION_POINTERS* pstExcptionInfor) :
m_nExceptionCode(0),
m_pExcptionInfor(NULL),
m_szMsgBuff(NULL),
m_hNtModule(NULL),
RtlNtStatusToDosError(NULL)
{
m_nExceptionCode = nExceptionCode;
m_pExcptionInfor = pstExcptionInfor;
m_hNtModule = GetModuleHandle(_T("NTDLL.DLL"));
if (NULL != m_hNtModule)
{
RtlNtStatusToDosError = (fpRtlNtStatusToDosError)GetProcAddress(m_hNtModule, "RtlNtStatusToDosError");
}
}
virtual ~seh_excpetion()
{
m_nExceptionCode = 0;
m_pExcptionInfor = NULL;
RtlNtStatusToDosError = NULL;
if (NULL != m_szMsgBuff)
{
LocalFree(m_szMsgBuff);
m_szMsgBuff = NULL;
}
};
const char* what() const noexcept
{
if (RtlNtStatusToDosError != NULL)
{
DWORD nConvertLen = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE,
m_hNtModule,
RtlNtStatusToDosError(m_nExceptionCode),
0,
(char*)&m_szMsgBuff,
0,
NULL);
if (0 != nConvertLen)
{
return m_szMsgBuff;
}
}
return "SEH_UNKNOW_ERROR";
}
const PEXCEPTION_POINTERS info() const
{
return m_pExcptionInfor;
}
const unsigned int code() const
{
return m_nExceptionCode;
}
private:
HMODULE m_hNtModule;
unsigned int m_nExceptionCode;
char* m_szMsgBuff;
PEXCEPTION_POINTERS m_pExcptionInfor;
fpRtlNtStatusToDosError RtlNtStatusToDosError;
public:
static void(__cdecl TranslateSEHtoCE)(unsigned int nExceptionCode, struct _EXCEPTION_POINTERS* pstExcptionInfor)
{
throw seh_excpetion(nExceptionCode, pstExcptionInfor);
}
};