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

  1.                                     -- Chapter 17 - Program 5
  2. package Stuff is
  3.    Funny_Add_Error : exception;
  4.    procedure Add_One(Number : in out INTEGER);
  5.    function Subtract_One(Number : INTEGER) return INTEGER;
  6. end Stuff;
  7.  
  8.  
  9.  
  10. with Text_IO;
  11. use Text_IO;
  12.  
  13. package body Stuff is
  14.  
  15.    procedure Add_One(Number : in out INTEGER) is
  16.    begin
  17.       Number := Number + 1;
  18.    exception
  19.       when Funny_Add_Error => Put_Line("Funny add error raised");
  20.       when Numeric_Error => Put_Line("Numeric error raised");
  21.    end Add_One;
  22.  
  23.    function Subtract_One(Number : INTEGER) return INTEGER is
  24.       Funny_Subtract_Error : exception;
  25.    begin
  26.       raise Funny_Subtract_Error;
  27.       return Number - 1;
  28.    exception
  29.       when Funny_Add_Error =>
  30.                          Put_Line("Funny add error raised");
  31.                          raise;
  32.       when Funny_Subtract_Error =>
  33.                          Put_Line("Funny subtract error raised");
  34.                          raise;
  35.    end Subtract_One;
  36.  
  37. begin
  38.    null;
  39. exception
  40.    when Numeric_Error =>
  41.                     Put_Line("Numeric error during elaboration");
  42.    when Constraint_Error =>
  43.                  Put_Line("Constraint_Error during elaboration");
  44.    when Funny_Add_Error =>
  45.                   Put_Line("Funny Add error during elaboration");
  46. -- when Funny_Subtract_Error =>
  47. --           Put_Line("Funny subtract error during elaboration");
  48. end Stuff;
  49.  
  50.  
  51.  
  52. with Text_IO, Stuff;
  53. use Text_IO, Stuff;
  54.  
  55. procedure Except5 is
  56.    Index : INTEGER := 5;
  57.    Add_Error : exception renames Stuff.Funny_Add_Error;
  58. begin
  59.    Add_One(Index);
  60.    Index := Subtract_One(Index);
  61. exception
  62.    when Numeric_Error | Constraint_Error =>
  63.              Put_Line("Numeric error or constraint error raised.");
  64.    when Add_Error =>
  65.              Put_Line("Addition error detected");
  66.    when others =>
  67.              Put_Line("An unknown exception raised");
  68.              raise;      -- Raise it again for the operating system
  69. end Except5;
  70.  
  71.  
  72.  
  73.  
  74. -- Result of execution
  75.  
  76. -- Funny subtract error raised
  77. -- An unknown exception raised
  78. -- Unhandled exception; funny_subtract_error
  79.  
  80.