把SEH类型的系统异常转化为C++类型的异常
来源:优易学  2010-1-14 19:24:39   【优易学:中国教育考试门户网】   资料下载   IT书店

  做一件事情之前,我们最好要搞清为什么!“十万个为什么” 可曾造就了多少顶级奇才!呵呵! WHY? ? WHY ? WHY ?这对任何一个人来说,都绝对是个好习惯,阿愚同学就一直把这个当“宝贝”。那么,究竟 为什么要 把 SEH 类型的系统异常转化为 C++ 类型的异常?朋友们,大家都想想,整理整理自己的意见和想法。这里,阿愚给出它个人的理解,如下:
  • 首先是由于我们在编程时,仍然最好遵循 MSDN 给出的建议和忠告(“ C 程序中使用 try-except 和 try-finally ;而 C++ 程序则应该使用 try-catch ”)。但是,为了提高程序的可靠性(防止意外系统异常的出现,而导致的程序无规则崩溃现象),我们还必须要求在 C++ 程序中,使用 try-catch 模型 能够捕获并 处理系统异常, 这在第 3 集的文章中曾详细讨论过了它, 那就是它只能采用 catch(…) 语法来捕获 SEH 类型的系统异常。 catch(…) 的使用虽然一定程度上提高了程序的可靠性,但是,“异常发生时的许多相关信息”它却什么也没有提供给程序员(包括何种类型的系统异常,出现的地点,以及其它有关异常的信息等等)。因此,我们需要一种有效途径,来把 SEH 类型的系统异常转化为 C++ 类型的异常?这无疑也就提供了一种在 C++ 异常处理模型中,不仅能够处理系统异常,而且还能够获取有关 SEH 类型系统异常中的许多详细信息的手段。
  • 其次就是,阿愚多次阐述过, C++ 异常处理是和面向对象紧密联系的(它二位可是“哥俩好”),因此,如果把 SEH 类型的系统异常统一到面向对象体系结构设计的“异常分类”中去,那对程序员而言,岂不是妙哉!美哉!该解决方案真所谓是,即提高了可靠性;又不失优雅!
  如何实现 把 SEH 类型的系统异常转化为 C++ 类型的异常?
  虽然说把 SEH 类型的系统异常转化为 C++ 类型的异常,给 C++ 程序员带来的是好处多多,但是实现起来并不复杂,因为系统底层和 VC 运行库已经为我们铺路搭桥了,也即我们可以通过 VC 运行库中的“ _ set_se_translator ”函数来轻松搞定它。 MSDN 中对它解释如下:
  Handles Win32 exceptions (C structured exceptions) as C++ typed exceptions.
  typedef void (*_se_translator_function)( unsigned int, struct _EXCEPTION_POINTERS* );
  _se_translator_function _set_se_translator( _se_translator_function se_trans_func );
  The _set_se_translator function provides a way to handle Win32 exceptions (C structured exceptions) as C++ typed exceptions. To allow each C exception to be handled by a C++ catch handler, first define a C exception “wrapper” class that can be used, or derived from, in order to attribute a specific class type to a C exception. To use this class, install a custom C exception translator function that is called by the internal exception-handling mechanism each time a C exception is raised. Within your translator function, you can throw any typed exception that can be caught by a matching C++ catch handler.
  To specify a custom translation function, call _set_se_translator with the name of your translation function as its argument. The translator function that you write is called once for each function invocation on the stack that has try blocks. There is no default translator function.
  In a multithreaded environment, translator functions are maintained separately for each thread. Each new thread needs to install its own translator function. Thus, each thread is in charge of its own translation handling.
  The se_trans_func function that you write must take an unsigned integer and a pointer to a Win32 _EXCEPTION_POINTERS structure as arguments. The arguments are the return values of calls to the Win32 API GetExceptionCode and GetExceptionInformation functions, respectively.
  至于 _ set_se_translator 函数的具体机制,以及 把系统异常转化为 C++ 类型的异常的原理这里不再详细讨论,而仅仅是给出了大致的工作流程: 首先,通过 _ set_se_translator 函数设置一个对所有的 Windows 系统异常产生作用的回调处理函数(也是与 TLS 数据有关);因此,每当程序运行时产生了系统异常之后,前面我们设置的自定义回调函数于是便会接受程序的控制权;接着,我们在该函数的实现中,可以根据不同类型的系统异常( EXCEPTION_POINTERS ),来分别抛出一个 C++ 类型的异常错误。呵呵!简单吧!不再白话了,还是来瞧瞧阿愚所设计的一个简单演示例程吧!代码如下:
  // FILENAME:SEH-test.cpp
  #include <windows.h>
  #include <cstdio>
  #include <eh.h>
  #include <string>
  #include <exception>
  using namespace std;
  ////////////////////////////////////////////////////////////////////////////////
  class seh_exception_base : public std::exception
  {
  public:
  seh_exception_base (const PEXCEPTION_POINTERS pExp, std::string what )
  : m_ExceptionRecord(*pExp->ExceptionRecord),
  m_ContextRecord(*pExp->ContextRecord),
  m_what(what){};
  ~seh_exception_base() throw(){} ;
  virtual const char* what() const throw()
  {
  return m_what.c_str();
  }
  virtual DWORD exception_code() const throw()
  {
  return m_ExceptionRecord.ExceptionCode;
  }
  virtual const EXCEPTION_RECORD& get_exception_record() const throw()
  {
  return m_ExceptionRecord;
  }
  virtual const CONTEXT& get_context() const throw()
  {
  return m_ContextRecord;
  }

[1] [2] [3] 下一页

责任编辑:cyth

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训