home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / except3.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  1.9 KB  |  73 lines

  1.                                        -- Chapter 17 - Program 3
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure Except3 is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    procedure Divide_By_Zero(Count : INTEGER) is
  11.       Divide_Result : INTEGER;
  12.    begin
  13.       Put("Count is");
  14.       Put(Count,3);
  15.       Put(" and the answer is");
  16.       Divide_Result := 25/(Count - 4);
  17.       Put(Divide_Result,3);
  18.       New_Line;
  19.    exception
  20.       when Numeric_Error => Put_Line(" Divide by zero occurred");
  21.    end Divide_By_Zero;
  22.  
  23.    procedure Raise_An_Error(Count : INTEGER) is
  24.       My_Own_Error : exception;
  25.       Another_Result : INTEGER;
  26.    begin
  27.       Put("Count is");
  28.       Put(Count,3);
  29.       Another_Result := 35/(Count - 6);  -- untested divide by zero
  30.       if Count = 3 then
  31.          raise My_Own_Error;
  32.       end if;
  33.       Put_Line(" and is a legal value");
  34.    exception
  35.       when My_Own_Error => Put_Line(" My own error occurred");
  36.    end Raise_An_Error;
  37.  
  38. begin
  39.    Put_Line("Begin program here.");
  40.    for Count in 1..7 loop
  41.       Divide_By_Zero(Count);
  42.       Raise_An_Error(Count);
  43.    end loop;
  44.    Put_Line("End of program.");
  45.  
  46.    exception
  47.       when Numeric_Error => Put(" Numeric error detected at the");
  48.                             Put_Line(" main program level.");
  49.                             Put_Line("Program terminated.");
  50. end Except3;
  51.  
  52.  
  53.  
  54.  
  55. -- Result of Execution
  56.  
  57. -- Begin program here.
  58. -- Count is  1 and the answer is -8
  59. -- Count is  1 and is a legal value
  60. -- Count is  2 and the answer is-12
  61. -- Count is  2 and is a legal value
  62. -- Count is  3 and the answer is-25
  63. -- Count is  3 My own error occurred
  64. -- Count is  4 and the answer is Divide by zero occurred
  65. -- Count is  4 and is a legal value
  66. -- Count is  5 and the answer is 25
  67. -- Count is  5 and is a legal value
  68. -- Count is  6 and the answer is 12
  69. -- Count is  6 Numeric error detected at the main program level.
  70. -- Program terminated.
  71.  
  72.  
  73.