c++中的try...catch能否捕获如下的内存访问错误 ?
我在一个程序中有类似如下的方式捕获异常,但是碰上这种空指针的内存访问错误,
程序却直接崩溃而不会进入catch模块。难道这种错误使用catch(...)是无法捕获的,
有没有其他异常类型可以捕获到这种错误,有经验的大虾请进来指点一下try
{
char *p;
char buf[32] = {0};
p = NULL;strcpy(buf, p); //p为空指针,内存访问错误
}
catch(...)
{
cout<< "something error" << endl;
}
我在一个程序中有类似如下的方式捕获异常,但是碰上这种空指针的内存访问错误,
程序却直接崩溃而不会进入catch模块。难道这种错误使用catch(...)是无法捕获的,
有没有其他异常类型可以捕获到这种错误,有经验的大虾请进来指点一下try
{
char *p;
char buf[32] = {0};
p = NULL;strcpy(buf, p); //p为空指针,内存访问错误
}
catch(...)
{
cout<< "something error" << endl;
}
#include <windows.h>
#include<iostream>
#include <tchar.h>
using namespace std;LONG WINAPI MyUnhandledExceptionFilter(_EXCEPTION_POINTERS* lpExceptionInfo)
{
EXCEPTION_RECORD* pInfo = lpExceptionInfo->ExceptionRecord;
DWORD dwExceptionCode = pInfo->ExceptionCode; switch(dwExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
{
TCHAR szText[256] = {0};
_stprintf(szText, _T("Unknown Exception: 0x%x at address 0x%x"),
pInfo->ExceptionCode, pInfo->ExceptionAddress);
cout<<szText<<endl;
}
break;
default:
break;
}
return EXCEPTION_EXECUTE_HANDLER;
}
int main()
{
SetUnhandledExceptionFilter(MyUnhandledExceptionFilter); char *p;
char buf[32] = {0};
p = NULL;
strcpy(buf, p); //p为空指针,内存访问错误
return 0;
}