home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d179 / excption.lha / Excption / fact.c < prev    next >
C/C++ Source or Header  |  1989-02-25  |  2KB  |  84 lines

  1. /**************************************************************************/
  2. /*                                                                        */
  3. /*                                                                        */
  4. /*                                                                        */
  5. /*                EXCEPTION EXAMPLE                                       */
  6. /*               ==========================================               */
  7. /*                                                                        */
  8. /*                                                                        */
  9. /*  MODULE      : Exception                                               */
  10. /*  NOM         : fact.c                                                  */
  11. /*  FONCTION    :                                                         */
  12. /*                                                                        */
  13. /*  RESPONSABLE : HEWES Gerald                                            */
  14. /*  TEL         : 33 (1) 46 24 20 27                                      */
  15. /*                                                                        */
  16. /**************************************************************************/
  17.  
  18. /**************************************************************************/
  19. /*                                                                        */
  20. /* HEW 880324 Example file of exception handler                           */
  21. /*                                                                        */
  22. /**************************************************************************/
  23.  
  24.  
  25.  
  26. #include <stdio.h>
  27. #include "local:excption.h"
  28. #include <proto/exec.h>
  29.  
  30. ExcpGlobal;
  31. static  ExcpClass FACT;
  32.  
  33. main()
  34. {
  35.   int n            ;
  36.   int result       ;
  37.  
  38.   printf("Factorial?\n");
  39.   scanf("%d",&n);
  40.   FACT = AllocException(-1);
  41.   if (FACT == -1) exit(20);
  42.  
  43.   MAIN
  44.    {
  45.      puts("protected code\n");
  46.      result = fact(n);
  47.    }
  48.   EXCEPTION
  49.    {
  50.      puts("Handler\n");
  51.      printf("Exception : %d \n",Eclass);
  52.    }
  53.   OUT
  54.   printf("%d\n",result);
  55.   puts("Done\n");
  56.   FreeException(FACT);
  57.   return(0);
  58. }
  59.  
  60.  
  61. int fact(n)
  62. int n;
  63.  
  64. {
  65.  ExcpDeclare;
  66.  int result;
  67.  
  68.  BEGIN
  69.   {
  70.    if (n==0) RAISE(FACT);
  71.    result = n*fact(n-1);
  72.    if (result > 1000000000) RAISE(NUMERIC_ERROR);
  73.   }
  74.  EXCEPTION
  75.   {
  76.    if (Eclass == FACT) result = 1;
  77.    else RAISE(Eclass);
  78.   }
  79.  END
  80.  
  81.  return result;
  82. }
  83.  
  84.