假设 C 程序可能会触发操作码异常,0xCC
我该如何捕捉它?
我试过:
__try
{
...some code
}
__except(GetExceptionCode()==EXCEPTION_EXECUTE_HANDLER)
{
_tprintf(_T("Error\n"),i);
return 0;
}
这对我不起作用。我究竟做错了什么?谢谢!
您没有检查正确的异常代码。
int 3
抛出EXCEPTION_SINGLE_STEP
。
你这样处理:
__try
{
// some code that might cause an int3
}
__except(GetExceptionCode() == EXCEPTION_SINGLE_STEP ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
// error handling of int3
}
编辑:请注意,在附加调试器的情况下运行的代码不会看到异常,因为调试器会处理它并在将手传回代码之前将其清除。
由于这是一个 win32 程序,您始终可以设置自己的“crashhandler” 这是我的一个程序的粗略片段。使用风险自负;)
LONG WINAPI crashhandler(struct _EXCEPTION_POINTERS* ExceptionInfo);
SetErrorMode(SEM_FAILCRITICALERRORS);
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)crashhandler);
LONG WINAPI crashhandler(struct _EXCEPTION_POINTERS* ExceptionInfo)
{
DWORD exceptioncode;
char exceptiondesc[8192];
exceptioncode = ExceptionInfo->ExceptionRecord->ExceptionCode;
switch(exceptioncode)
{
case EXCEPTION_ACCESS_VIOLATION:
strcpy(exceptiondesc,"EXCEPTION_ACCESS_VIOLATION:");
break;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
strcpy(exceptiondesc,"EXCEPTION_ARRAY_BOUNDS_EXCEEDED:");
break;
case EXCEPTION_BREAKPOINT:
strcpy(exceptiondesc,"EXCEPTION_BREAKPOINT:");
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
strcpy(exceptiondesc,"EXCEPTION_DATATYPE_MISALIGNMENT:");
break;
case EXCEPTION_FLT_DENORMAL_OPERAND:
strcpy(exceptiondesc,"EXCEPTION_FLT_DENORMAL_OPERAND:");
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
strcpy(exceptiondesc,"EXCEPTION_FLT_DIVIDE_BY_ZERO:");
break;
case EXCEPTION_FLT_INEXACT_RESULT:
strcpy(exceptiondesc,"EXCEPTION_FLT_INEXACT_RESULT:");
break;
case EXCEPTION_FLT_INVALID_OPERATION:
strcpy(exceptiondesc,"EXCEPTION_FLT_INVALID_OPERATION:");
break;
case EXCEPTION_FLT_OVERFLOW:
strcpy(exceptiondesc,"EXCEPTION_FLT_OVERFLOW:");
break;
case EXCEPTION_FLT_STACK_CHECK:
strcpy(exceptiondesc,"EXCEPTION_FLT_STACK_CHECK:");
break;
case EXCEPTION_FLT_UNDERFLOW:
strcpy(exceptiondesc,"EXCEPTION_FLT_UNDERFLOW:");
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
strcpy(exceptiondesc,"EXCEPTION_ILLEGAL_INSTRUCTION:");
break;
case EXCEPTION_IN_PAGE_ERROR:
strcpy(exceptiondesc,"EXCEPTION_IN_PAGE_ERROR:");
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
strcpy(exceptiondesc,"EXCEPTION_INT_DIVIDE_BY_ZERO:");
break;
case EXCEPTION_INT_OVERFLOW:
strcpy(exceptiondesc,"EXCEPTION_INT_OVERFLOW:");
break;
case EXCEPTION_INVALID_DISPOSITION:
strcpy(exceptiondesc,"EXCEPTION_INVALID_DISPOSITION:");
break;
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
strcpy(exceptiondesc,"EXCEPTION_NONCONTINUABLE_EXCEPTION:");
break;
case EXCEPTION_PRIV_INSTRUCTION:
strcpy(exceptiondesc,"EXCEPTION_PRIV_INSTRUCTION:");
break;
case EXCEPTION_SINGLE_STEP:
strcpy(exceptiondesc,"EXCEPTION_SINGLE_STEP:");
break;
case EXCEPTION_STACK_OVERFLOW:
strcpy(exceptiondesc,"EXCEPTION_STACK_OVERFLOW:");
break;
default:
strcpy(exceptiondesc,"Unknown exception\n");
break;
}
printf("** --- CRASH BANG BOOM ---\n");
printf("** Exception : 0x%08x\n",exceptioncode);
printf("** Exception description : %s\n" ,exceptiondesc);
printf("** Exception flags : 0x%08x\n",ExceptionInfo->ExceptionRecord->ExceptionFlags);
printf("** Exception address : 0x%08x\n",ExceptionInfo->ExceptionRecord->ExceptionAddress);
printf("**\n");
//return EXCEPTION_EXECUTE_HANDLER;
//return EXCEPTION_CONTINUE_EXECUTION;
//return EXCEPTION_CONTINUE_SEARCH;
return 0;
}