Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 3.27 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. VS2008 Enable C   Exception with SEH
  2. bool ignore_exception(unsigned pCode)
  3. {
  4.     const unsigned ignoreList[] = {EXCEPTION_BREAKPOINT,
  5.         EXCEPTION_FLT_DENORMAL_OPERAND, EXCEPTION_FLT_DIVIDE_BY_ZERO,
  6.         EXCEPTION_FLT_INEXACT_RESULT, EXCEPTION_FLT_OVERFLOW, EXCEPTION_FLT_UNDERFLOW,
  7.         EXCEPTION_INT_OVERFLOW, EXCEPTION_SINGLE_STEP};
  8.  
  9.     auto result = std::search_n(std::begin(ignoreList), std::end(ignoreList),
  10.                     1, pCode);
  11.     return result != std::end(ignoreList);              
  12. }
  13.  
  14. std::string code_string(unsigned pCode)
  15. {
  16.     switch (pCode)
  17.     {
  18.     case EXCEPTION_ACCESS_VIOLATION:
  19.         return "Access violation";
  20.     case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  21.         return "Out of array bounds";
  22.     case EXCEPTION_BREAKPOINT:
  23.         return "Breakpoint";
  24.     case EXCEPTION_DATATYPE_MISALIGNMENT:
  25.         return "Misaligned data";
  26.     case EXCEPTION_FLT_DENORMAL_OPERAND:
  27.         return "Denormalized floating-point value";
  28.     case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  29.         return "Floating-point divide-by-zero";
  30.     case EXCEPTION_FLT_INEXACT_RESULT:
  31.         return "Inexact floating-point value";
  32.     case EXCEPTION_FLT_INVALID_OPERATION:
  33.         return "Invalid floating-point operation";
  34.     case EXCEPTION_FLT_OVERFLOW:
  35.         return "Floating-point overflow";
  36.     case EXCEPTION_FLT_STACK_CHECK:
  37.         return "Floating-point stack overflow";
  38.     case EXCEPTION_FLT_UNDERFLOW:
  39.         return "Floating-point underflow";
  40.     case EXCEPTION_GUARD_PAGE:
  41.         return "Page-guard access";
  42.     case EXCEPTION_ILLEGAL_INSTRUCTION:
  43.         return "Illegal instruction";
  44.     case EXCEPTION_IN_PAGE_ERROR:
  45.         return "Invalid page access";
  46.     case EXCEPTION_INT_DIVIDE_BY_ZERO:
  47.         return "Integer divide-by-zero";
  48.     case EXCEPTION_INT_OVERFLOW:
  49.         return "Integer overflow";
  50.     case EXCEPTION_INVALID_DISPOSITION:
  51.         return "Invalid exception dispatcher";
  52.     case EXCEPTION_INVALID_HANDLE:
  53.         return "Invalid handle";
  54.     case EXCEPTION_NONCONTINUABLE_EXCEPTION:
  55.         return "Non-continuable exception";
  56.     case EXCEPTION_PRIV_INSTRUCTION:
  57.         return "Invalid instruction";
  58.     case EXCEPTION_SINGLE_STEP:
  59.         return "Single instruction step";
  60.     case EXCEPTION_STACK_OVERFLOW:
  61.         return "Stack overflow";
  62.     default:
  63.         return "Unknown exception";
  64.     }
  65. }
  66.  
  67. void stack_fail_thread()
  68. {
  69.     std::cerr << "Unhandled exception:n"
  70.                 << code_string(EXCEPTION_STACK_OVERFLOW) << 'n';
  71.     std::cerr << "Terminating." << std::endl;
  72.  
  73.     // can print a stack dump of the failed
  74.     // thread to see what went wrong, etc...
  75.  
  76.     std::exit(EXIT_FAILURE);
  77. }
  78.  
  79. void exception_translator(unsigned pCode, _EXCEPTION_POINTERS*)
  80. {
  81.     // minimize function calls if it's a stack overflow
  82.     if (pCode == EXCEPTION_STACK_OVERFLOW)
  83.     {
  84.         // do some additional processing in another thread,
  85.         // because the stack of this thread is gone
  86.         boost::thread t(stack_fail_thread);
  87.         t.join(); // will never exit
  88.     }
  89.     else if (!ignore_exception(pCode))              
  90.     {
  91.         // can add a stack dump to the exception message,
  92.         // since these tend to be pretty severe, etc...
  93.         BOOST_THROW_EXCEPTION(std::runtime_error(code_string(pCode)));
  94.     }
  95. }
  96.  
  97. void hook_signals()
  98. {
  99.     _set_se_translator(exception_translator);
  100. }