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

本文探讨了Windows异常处理与C++异常处理的区别。Windows异常主要处理非法内存访问和指令错误,由内核捕获并传递给用户层,而C++异常是用户自定义类型,通过堆栈展开实现。由于两者处理方式相似,混用可能导致局部堆栈冲突。__try/__finally在C++中不检查构造和析构,而try/catch则不会出现此类问题,并且需要用户自定义所有异常类型。

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!");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值