VC异常.简单抛,简单捕获

1、ZC:始终没有找到,能像Delphi7里面那样能抛 字符串描述 的异常信息。(难道Delphi是这样做的?:在程序的最外围 套了一层异常捕获,然后在获取到异常之后,自己再将异常信息弹出来,我们写的字符串信息 可以通过 _EXCEPTION_RECORD.ExceptionInformation来保存)[ 下面有我的猜测Delphi7中的做法 ]

  ZC:这里就 使用 异常Code 来区分异常吧。本来想使用 0xFFFF???? 来表示 我的异常Code的,但是 实际运行发现 跑出来的异常代码变成了 0xEFFF????,于是改为使用 0xCCCC????,这个看了一下 没有变化...

2、简单 抛异常 WindowsAPI:

RaiseException(
__in DWORD dwExceptionCode,
__in DWORD dwExceptionFlags,
__in DWORD nNumberOfArguments,
__in_ecount_opt(nNumberOfArguments) CONST ULONG_PTR *lpArguments
);

  2.1、我的抛异常使用:RaiseException(0xCCCC????, EXCEPTION_NONCONTINUABLE, 0, NULL);

    ZC:第二个参数 如果不是EXCEPTION_NONCONTINUABLE,而是 0 的话  表示(exe)可以继续执行

  2.2、我的捕获异常的使用:

void main()
{
__try
{
RaiseException(0xCCCC0001, EXCEPTION_NONCONTINUABLE, , NULL);
}
__except( ExceptionFilterZ( "main(...)", GetExceptionCode() ) )
{
printf("Catch an exception\n");
} printf("main() out.\n");
system("pause");
}
long WINAPI ExceptionFilterZ(char* _pcFuncName, DWORD _dwExceptionCode)
{
//return (_dwExceptionCode == STATUS_STACK_OVERFLOW) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; printf_s("==> ==> ==> %s catch exception , _dwExceptionCode : %d(0x%08X)\n",
_pcFuncName, _dwExceptionCode, _dwExceptionCode); return EXCEPTION_EXECUTE_HANDLER; //#define STATUS_SEGMENT_NOTIFICATION ((DWORD )0x40000005L)
//STATUS_STACK_OVERFLOW
}

3、我猜 Delphi7中可能是这样做的:

  通过 struct _EXCEPTION_POINTERS 保存程序员输入的 字符串信息,在异常发生时 再将它取出来显示

  3.1、测试代码:

long WINAPI ExceptionFilterFunc(char* _pcFuncName, struct _EXCEPTION_POINTERS *_exception_info)
{
//return (_dwExceptionCode == STATUS_STACK_OVERFLOW) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; DWORD dwExceptionCode = _exception_info->ExceptionRecord->ExceptionCode;
printf_s("==> ==> ==> Catch exception !!! Func name :%s, _dwExceptionCode : %d(0x%08X)\n",
_pcFuncName, dwExceptionCode, dwExceptionCode); DWORD dwNumberParameters = _exception_info->ExceptionRecord->NumberParameters;
printf("\t NumberParameters :%d\n", dwNumberParameters);
if (dwNumberParameters == )
{
ULONG_PTR ulp = _exception_info->ExceptionRecord->ExceptionInformation[];
char* pc = (char*)ulp;
printf("\t Information :%s\n", pc);
delete[] pc;
} return EXCEPTION_EXECUTE_HANDLER; //#define STATUS_SEGMENT_NOTIFICATION ((DWORD )0x40000005L)
//STATUS_STACK_OVERFLOW
} void main()
{
__try
{
char* pcMsg = "zc test exception msg .";
char* pc = new char[strlen(pcMsg) + ];
pc[strlen(pcMsg)] = '\0';
memcpy(&pc[], &pcMsg[], strlen(pcMsg));
ULONG_PTR ulps[EXCEPTION_MAXIMUM_PARAMETERS] = {};
ulps[] = (ULONG_PTR)pc;
RaiseException(0xCCCC0001, EXCEPTION_NONCONTINUABLE, , &ulps[]); //GetExceptionInformation (struct _EXCEPTION_POINTERS *)_exception_info
}
__except( ExceptionFilterFunc( "main(...)", GetExceptionInformation() ) )
{
printf("Catch an exception\n");
} printf("main() out.\n");
system("pause");
}

4、

5、

上一篇:Android Weekly Notes Issue #254


下一篇:WCF利用Stream上传大文件