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

  1. //
  2. // EXCEP3.C -- Sample structured exception handler
  3. //             Demonstrates nested "try" blocks
  4. //
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <pharlap.h>
  8. #define i386 1
  9. #define WIN32
  10. #include <windows.h>
  11. #include <excpt.h>
  12.  
  13. void Level2(void);
  14. int Level2Filter(ULONG ExcepCode, struct _EXCEPTION_POINTERS *pExcepInfo);
  15. void Level3(void);
  16. int Level3Filter(ULONG ExcepCode);
  17. void CauseExceptions(void);
  18.  
  19. int main()
  20. {
  21.     int    ExcepCode;
  22.  
  23.     try
  24.     {
  25.         Level2();
  26.     }
  27.     except (EXCEPTION_EXECUTE_HANDLER)
  28.     {
  29.         ExcepCode = GetExceptionCode();
  30.         if (ExcepCode == EXCEPTION_ACCESS_VIOLATION)
  31.             printf("main - got GP fault, terminating program\n");
  32.         else
  33.             printf("In main - unexpected exception: %08Xh\n",
  34.                             ExcepCode);
  35.         exit(1);
  36.     }
  37.  
  38.     printf("Normal termination\n");
  39.     return 0;
  40. }
  41.  
  42. void Level2(void)
  43. {
  44.     try
  45.     {
  46.         Level3();
  47.     }
  48.     except (Level2Filter(GetExceptionCode(),GetExceptionInformation()))
  49.     {
  50.         // shouldn't get here, we never say EXCEPTION_EXECUTE_HANDLER
  51.         printf("In Level 2 exception handler:  terminating program\n");
  52.         exit(1);
  53.     }
  54. }
  55.  
  56. int Level2Filter(ULONG ExcepCode, struct _EXCEPTION_POINTERS *pExcepInfo)
  57. {
  58.     EXCEPTION_RECORD *pExcepRecord;
  59.     CONTEXT *pContext;
  60.  
  61.     pExcepRecord = pExcepInfo->ExceptionRecord;
  62.     pContext = pExcepInfo->ContextRecord;
  63.  
  64.     if (ExcepCode == EXCEPTION_INT_DIVIDE_BY_ZERO)
  65.     {
  66.         printf("Level 2 - Got divide-by-zero exception\n");
  67.         if (!pContext->ContextFlags & CONTEXT_CONTROL)
  68.         {
  69.             printf("  No context -- let system handler run\n");
  70.             return EXCEPTION_CONTINUE_SEARCH;
  71.         }
  72.         printf("  Incrementing EIP past DIV and ignoring it\n");
  73.         pContext->Eip += 2;
  74.         return EXCEPTION_CONTINUE_EXECUTION;
  75.     }
  76.     else
  77.     {
  78.         printf("Level 2 - Can't handle exception: %08Xh\n", ExcepCode);
  79.         return EXCEPTION_CONTINUE_SEARCH;
  80.     }
  81. }
  82.  
  83. void Level3(void)
  84. {
  85.     try
  86.     {
  87.         CauseExceptions();
  88.     }
  89.     except (Level3Filter(GetExceptionCode()))
  90.     {
  91.         // shouldn't get here, we never say EXCEPTION_EXECUTE_HANDLER
  92.         printf("In Level 3 exception handler:  terminating program\n");
  93.         exit(1);
  94.     }
  95. }
  96.  
  97. int Level3Filter(ULONG ExcepCode)
  98. {
  99.     if (ExcepCode == EXCEPTION_INT_OVERFLOW)
  100.     {
  101.         printf("Level 3 - ignoring integer overflow\n");
  102.         return EXCEPTION_CONTINUE_EXECUTION;
  103.     }
  104.     else
  105.     {
  106.         printf("Level 3 - Can't handle exception: %08Xh\n", ExcepCode);
  107.         return EXCEPTION_CONTINUE_SEARCH;
  108.     }
  109. }
  110.  
  111. int VarInDataSeg;
  112. void CauseExceptions(void)
  113. {
  114.     _asm
  115.     {
  116.         mov    eax,7FFFFFFFh
  117.         mov    ebx,7FFFFFFFh
  118.         add    eax,ebx
  119.         into            ; integer overflow
  120.     }
  121.  
  122.     _asm
  123.     {
  124.         xor    ebx,ebx
  125.         div    ebx        ; divide by zero
  126.     }
  127.  
  128.     _asm
  129.     {
  130.         push    0
  131.         pop    ds        ; set DS to null selector
  132.         mov    VarInDataSeg,0    ; GP fault, because DS is null
  133.     }
  134.     return;
  135. }
  136.