CSDN博客

img wxtsmart

结构化异常处理和C++的异常处理区别

发表于2008/9/28 17:54:00  1777人阅读

分类: VC/MFC 源代码 C/C++

Windows异常由系统内核代码捕获,然后传递至用户层,多用于非法内存访问、指令错误等,其类型是CPU定义的,参见《X386保护模式编程》。
C++异常是C++编译器生成的结构(多在堆栈上),是普通的用户层代码,异常类型由用户自定义。
由于Windows异常处理和C++异常处理均使用堆栈展开,在局部堆栈的使用中存在冲突,一般不能混用。

__try{} __finally{}不会检查类内析构以及构造等操作
但是try() catch()不会存在这样的问题但是C++编译器上的所以异常类型需要你自己定义。

 

__try
 {
      ......
 }
 __except (EXCEPTION_EXECUTE_HANDLER)
 {
  wprintf(GetExceptionDescribe(GetExceptionCode()));
 }

 

 

#pragma once

typedef struct tagExcpCode
{
 DWORD dwCode;
 LPTSTR lpDescribe;
}EXCPCODE,*PEXCPCODE;


EXCPCODE g_excp_table[]=
{
 EXCEPTION_ACCESS_VIOLATION,
 _T("The thread attempts to read from or write to a virtual address for which it does not have access."),

 EXCEPTION_ARRAY_BOUNDS_EXCEEDED,
 _T("The thread attempts to access an array element that is out of bounds, and the underlying hardware supports bounds checking."),

 EXCEPTION_BREAKPOINT,
 _T("A breakpoint is encountered."),

 EXCEPTION_DATATYPE_MISALIGNMENT,
 _T("The thread attempts to read or write data that is misaligned on hardware that does not provide alignment. For example, 16-bit values must be aligned on 2-byte boundaries, 32-bit values on 4-byte boundaries, and so on."),

 EXCEPTION_FLT_DENORMAL_OPERAND,
 _T("One of the operands in a floating point operation is denormal. A denormal value is one that is too small to represent as a standard floating point value."),

 EXCEPTION_FLT_DIVIDE_BY_ZERO,
 _T("The thread attempts to divide a floating point value by a floating point divisor of 0 (zero)."),

 EXCEPTION_FLT_INEXACT_RESULT,
 _T("The result of a floating point operation cannot be represented exactly as a decimal fraction."),

 EXCEPTION_FLT_INVALID_OPERATION,
 _T("A floatin point exception that is not included in this list."),

 EXCEPTION_FLT_OVERFLOW,
 _T("The exponent of a floating point operation is greater than the magnitude allowed by the corresponding type."),

 EXCEPTION_FLT_STACK_CHECK,
 _T("The stack has overflowed or underflowed, because of a floating point operation."),

 EXCEPTION_FLT_UNDERFLOW,
 _T("The exponent of a floating point operation is less than the magnitude allowed by the corresponding type."),

 EXCEPTION_GUARD_PAGE,
 _T("The thread accessed memory allocated with the PAGE_GUARD modifier."),

 EXCEPTION_ILLEGAL_INSTRUCTION,
 _T("The thread tries to execute an invalid instruction."),

 EXCEPTION_IN_PAGE_ERROR,
 _T("The thread tries to access a page that is not present, and the system is unable to load the page. For example, this exception might occur if a network connection is lost while running a program over a network."),

 EXCEPTION_INT_DIVIDE_BY_ZERO,
 _T("The thread attempts to divide an integer value by an integer divisor of 0 (zero)."),

 EXCEPTION_INT_OVERFLOW,
 _T("The result of an integer operation causes a carry out of the most significant bit of the result."),

 EXCEPTION_INVALID_DISPOSITION,
 _T("An exception handler returns an invalid disposition to the exception dispatcher. Programmers using a high-level language such as C should never encounter this exception."),

 EXCEPTION_INVALID_HANDLE,
 _T("The thread used a handle to a kernel object that was invalid (probably because it had been closed.)"),

 EXCEPTION_NONCONTINUABLE_EXCEPTION,
 _T("The thread attempts to continue execution after a non-continuable exception occurs."),

 EXCEPTION_PRIV_INSTRUCTION,
 _T("The thread attempts to execute an instruction with an operation that is not allowed in the current computer mode."),

 EXCEPTION_SINGLE_STEP,
 _T("A trace trap or other single instruction mechanism signals that one instruction is executed."),

 EXCEPTION_STACK_OVERFLOW,
 _T("The thread uses up its stack."),

};
int g_excp_tableCount = sizeof(g_excp_table)/sizeof(g_excp_table[0]);

LPTSTR GetExceptionDescribe(DWORD dwCode)
{
 for (int i=0;i<g_excp_tableCount;i++)
 {
  if (g_excp_table[i].dwCode==dwCode)
  {
   return g_excp_table[i].lpDescribe;
  }
 }
 return _T("Unkown Exception!");
}

0 0

相关博文

我的热门文章

img
取 消
img