Errors in Ada

In Ada, it’s idiomatic to communicate errors via exceptions, which is different from the approach used in some other languages. Ada’s exception handling mechanism allows for robust error management and recovery.

with Ada.Text_IO;
with Ada.Exceptions;

procedure Error_Handling is

   -- Custom exception types
   No_More_Tea : exception;
   Cant_Boil_Water : exception;

   -- Function that may raise an exception
   function F(Arg : Integer) return Integer is
   begin
      if Arg = 42 then
         -- Raise a predefined exception with a custom message
         raise Constraint_Error with "can't work with 42";
      end if;
      return Arg + 3;
   end F;

   -- Procedure that may raise custom exceptions
   procedure Make_Tea(Arg : Integer) is
   begin
      if Arg = 2 then
         raise No_More_Tea;
      elsif Arg = 4 then
         -- We can raise an exception and provide additional context
         raise Cant_Boil_Water with "making tea";
      end if;
   end Make_Tea;

begin
   -- Example usage of F
   for I in 7 .. 42 loop
      begin
         declare
            Result : Integer;
         begin
            Result := F(I);
            Ada.Text_IO.Put_Line("F worked: " & Integer'Image(Result));
         end;
      exception
         when E : Constraint_Error =>
            Ada.Text_IO.Put_Line("F failed: " & Ada.Exceptions.Exception_Message(E));
      end;
   end loop;

   -- Example usage of Make_Tea
   for I in 0 .. 4 loop
      begin
         Make_Tea(I);
         Ada.Text_IO.Put_Line("Tea is ready!");
      exception
         when No_More_Tea =>
            Ada.Text_IO.Put_Line("We should buy new tea!");
         when E : Cant_Boil_Water =>
            Ada.Text_IO.Put_Line("Now it is dark. " & Ada.Exceptions.Exception_Message(E));
         when others =>
            Ada.Text_IO.Put_Line("Unknown error: " & Ada.Exceptions.Exception_Information(Ada.Exceptions.Get_Exception_Occurrence));
      end;
   end loop;
end Error_Handling;

In this Ada program:

  1. We define custom exception types No_More_Tea and Cant_Boil_Water.

  2. The F function demonstrates raising a predefined exception (Constraint_Error) with a custom message.

  3. The Make_Tea procedure shows how to raise custom exceptions.

  4. In the main procedure, we use exception handlers to catch and handle different types of exceptions.

  5. We use Ada.Exceptions.Exception_Message to get the message associated with an exception.

  6. The others handler catches any unhandled exceptions, demonstrating how to get detailed exception information.

Ada’s exception handling mechanism allows for separation of normal code flow and error handling, making the code easier to read and maintain. Unlike some languages that use return values for error handling, Ada uses exceptions, which can be caught and handled at any level of the call stack.

To run this program, save it as error_handling.adb and compile it with an Ada compiler:

$ gnatmake error_handling.adb
$ ./error_handling

This will compile the Ada program and then run it, displaying the output which demonstrates the various error handling scenarios.