Exception handling is a crucial aspect of programming that allows developers to manage and respond to runtime errors gracefully. In Ada, exception handling is robust and provides mechanisms to catch and handle errors, ensuring that programs can recover from unexpected situations without crashing.

Key Concepts

  1. Exceptions: Special conditions that alter the normal flow of execution.
  2. Raising Exceptions: Triggering an exception when an error condition is detected.
  3. Handling Exceptions: Catching and responding to exceptions to maintain program stability.
  4. Predefined Exceptions: Built-in exceptions provided by Ada.
  5. User-Defined Exceptions: Custom exceptions defined by the programmer.

Raising Exceptions

In Ada, exceptions can be raised using the raise statement. This can be done either explicitly by the programmer or implicitly by the runtime system.

Example: Raising an Exception

procedure Raise_Exception is
begin
   raise Constraint_Error;
end Raise_Exception;

In this example, the Constraint_Error exception is explicitly raised.

Handling Exceptions

To handle exceptions, Ada uses the exception block within a begin...end construct. This block catches exceptions and allows the programmer to define how to respond to them.

Example: Handling an Exception

procedure Handle_Exception is
begin
   raise Constraint_Error;
exception
   when Constraint_Error =>
      Ada.Text_IO.Put_Line("Constraint Error occurred!");
   when others =>
      Ada.Text_IO.Put_Line("An unexpected error occurred!");
end Handle_Exception;

In this example, the Constraint_Error is caught and handled by printing a message. The when others clause catches any other exceptions that are not explicitly handled.

Predefined Exceptions

Ada provides several predefined exceptions that cover common error conditions:

Exception Name Description
Constraint_Error Raised when a constraint is violated.
Program_Error Raised when a program error is detected.
Storage_Error Raised when there is a storage allocation error.
Tasking_Error Raised when a tasking error occurs.

Example: Using Predefined Exceptions

procedure Predefined_Exception is
   X : Integer := 0;
begin
   X := 1 / X;  -- This will raise a Constraint_Error
exception
   when Constraint_Error =>
      Ada.Text_IO.Put_Line("Division by zero error!");
end Predefined_Exception;

In this example, dividing by zero raises a Constraint_Error, which is then caught and handled.

User-Defined Exceptions

In addition to predefined exceptions, Ada allows programmers to define their own exceptions using the exception keyword.

Example: Defining and Raising a User-Defined Exception

procedure User_Defined_Exception is
   My_Exception : exception;
begin
   raise My_Exception;
exception
   when My_Exception =>
      Ada.Text_IO.Put_Line("My custom exception occurred!");
end User_Defined_Exception;

In this example, a custom exception My_Exception is defined and raised, then caught and handled.

Practical Exercises

Exercise 1: Handling Division by Zero

Write a procedure that takes two integers as input and performs division. Handle the case where the divisor is zero by raising and catching a Constraint_Error.

procedure Division_Handling is
   A, B : Integer;
begin
   A := 10;
   B := 0;
   Ada.Text_IO.Put_Line("Result: " & Integer'Image(A / B));
exception
   when Constraint_Error =>
      Ada.Text_IO.Put_Line("Error: Division by zero is not allowed.");
end Division_Handling;

Solution

procedure Division_Handling is
   A, B : Integer;
begin
   A := 10;
   B := 0;
   begin
      Ada.Text_IO.Put_Line("Result: " & Integer'Image(A / B));
   exception
      when Constraint_Error =>
         Ada.Text_IO.Put_Line("Error: Division by zero is not allowed.");
   end;
end Division_Handling;

Exercise 2: Custom Exception for Invalid Input

Define a custom exception for invalid input and write a procedure that raises this exception if the input is negative. Handle the exception by printing an appropriate message.

procedure Invalid_Input_Exception is
   Invalid_Input : exception;
   Input : Integer := -5;
begin
   if Input < 0 then
      raise Invalid_Input;
   end if;
exception
   when Invalid_Input =>
      Ada.Text_IO.Put_Line("Error: Invalid input, negative value not allowed.");
end Invalid_Input_Exception;

Solution

procedure Invalid_Input_Exception is
   Invalid_Input : exception;
   Input : Integer := -5;
begin
   if Input < 0 then
      raise Invalid_Input;
   end if;
exception
   when Invalid_Input =>
      Ada.Text_IO.Put_Line("Error: Invalid input, negative value not allowed.");
end Invalid_Input_Exception;

Common Mistakes and Tips

  • Forgetting to Handle Exceptions: Always ensure that exceptions are handled to prevent program crashes.
  • Using when others Wisely: The when others clause should be used to catch unexpected exceptions, but specific exceptions should be handled explicitly when possible.
  • Raising Exceptions Appropriately: Only raise exceptions for truly exceptional conditions, not for regular control flow.

Conclusion

Exception handling in Ada is a powerful feature that allows developers to manage errors gracefully. By understanding how to raise and handle both predefined and user-defined exceptions, you can write more robust and reliable programs. Practice the exercises provided to reinforce your understanding and prepare for more advanced topics in Ada programming.

© Copyright 2024. All rights reserved