home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l350 / 3.ddi / EXAMPLES / MSC / EXCEP2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-01  |  2.2 KB  |  95 lines

  1. //
  2. // EXCEP2.C -- Sample structured exception handler
  3. //             Causes an integer overflow exception, a divide by zero
  4. //             exception, and a GP (general protection) fault
  5. //
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <pharlap.h>
  9. #define i386 1
  10. #define WIN32
  11. #include <windows.h>
  12. #include <excpt.h>
  13.  
  14. void CauseExceptions(void);
  15. int ExceptionFilter(ULONG ExcepCode, struct _EXCEPTION_POINTERS *pExcepInfo);
  16.  
  17. int main()
  18. {
  19.     try
  20.     {
  21.         CauseExceptions();
  22.     }
  23.     except (ExceptionFilter(GetExceptionCode(),GetExceptionInformation()))
  24.     {
  25.         printf("In exception handler:  terminating program\n");
  26.         exit(1);
  27.     }
  28.  
  29.     printf("Normal termination\n");
  30.     return 0;
  31. }
  32.  
  33. int VarInDataSeg;
  34. void CauseExceptions(void)
  35. {
  36.     _asm
  37.     {
  38.         mov    eax,7FFFFFFFh
  39.         mov    ebx,7FFFFFFFh
  40.         add    eax,ebx
  41.         into            ; integer overflow
  42.     }
  43.  
  44.     _asm
  45.     {
  46.         xor    ebx,ebx
  47.         div    ebx        ; divide by zero
  48.     }
  49.  
  50.     _asm
  51.     {
  52.         push    0
  53.         pop    ds        ; set DS to null selector
  54.         mov    VarInDataSeg,0    ; GP fault, because DS is null
  55.     }
  56.     return;
  57. }
  58.  
  59. int ExceptionFilter(ULONG ExcepCode, struct _EXCEPTION_POINTERS *pExcepInfo)
  60. {
  61.     EXCEPTION_RECORD *pExcepRecord;
  62.     CONTEXT *pContext;
  63.  
  64.     pExcepRecord = pExcepInfo->ExceptionRecord;
  65.     pContext = pExcepInfo->ContextRecord;
  66.  
  67.     switch (ExcepCode)
  68.     {
  69. case EXCEPTION_INT_OVERFLOW:    // from INTO, ignore it and continue
  70.         printf("Got integer overflow exception -- ignoring it\n");
  71.         return EXCEPTION_CONTINUE_EXECUTION;
  72.  
  73. case EXCEPTION_INT_DIVIDE_BY_ZERO: // from DIV instruction
  74.         // Step the EIP past the 2-byte DIV opcode and continue
  75.         printf("Got divide-by-zero exception\n");
  76.         if (!pContext->ContextFlags & CONTEXT_CONTROL)
  77.         {
  78.             printf("  No context -- let system handler run\n");
  79.             return EXCEPTION_CONTINUE_SEARCH;
  80.         }
  81.         printf("  Incrementing EIP past DIV and ignoring it\n");
  82.         pContext->Eip += 2;
  83.         return EXCEPTION_CONTINUE_EXECUTION;
  84.  
  85. case EXCEPTION_ACCESS_VIOLATION: // from GP fault - execute handler
  86.         printf("Got GP fault -- letting except() handler run\n");
  87.         return EXCEPTION_EXECUTE_HANDLER;
  88.  
  89. default:    // not an exception this program catches; let default 
  90.         // system handler take it
  91.         printf("Unexpected exception code: %08Xh\n", ExcepCode);
  92.         return EXCEPTION_CONTINUE_SEARCH;
  93.     }
  94. }
  95.