next up previous contents index
Next: GNAT Implementation Up: Exceptions Previous: Exceptions   Contents   Index


Ada Model of Exceptions

An exception represents a kind of exceptional situation; an occurrence of such a situation (at run-time) is called an Exception Occurrence. To raise an exception is to abandon normal program execution thus drawing attention to the fact that the corresponding situation has arisen. Performing some actions in response to the arising of an exception is called Handling the exception [AAR95, section 11].

Exception Declaration

The syntax to declare an exception is [AAR95, section 11-1]:

  exception_declaration ::= defining_identifier_list : exception;

Raise Statement

The syntax to raise an exception is [AAR95, section 11-2]:

   raise_statement ::= raise [exception_name];

When an exception occurrence is raised by the execution of a given construct, the rest of the execution of that construct is abandoned; that is, any portions of the execution that have not yet taken place are not performed. A raise statement without an exception name is a re-raise statement and therefore must be within an exception handler.

Exception Handling

When an exception occurrence is raised, normal program execution is abandoned and control is transferred to an applicable exception handler, if any. To handle an exception occurrence is to respond to the exceptional event [AAR95, section 11-4]. The syntax to declare an exception handler is [AAR95, section 11-2]:

  handled_sequence_of_statements ::=

  exception_handler ::=
    when [choice_parameter_specification:]
         {| exception_choice} =>

  choice_parameter_specification ::= defining_identifier

  exception_choice ::= exception_name | others

If an exception occurrence is not handled (or is re-raised) it is propagated to the innermost dynamically enclosing execution [AAR95, section 11-4]. If an exception occurrence is unhanded in a task body, the exception does not propagate further (because there is no dynamically enclosing execution). If the exception occurred during the activation of the task, then the activator would raise Tasking_Error [AAR95, section 11-4].

Package Ada.Exceptions

The following language-defined library provides additional facilities for exceptions handling[]:

package Ada.Exceptions is

   type Exception_Id is private;
   Null_Id : constant Exception_Id;

   type Exception_Occurrence is limited private;
   type Exception_Occurrence_Access is access all Exception_Occurrence;

   Null_Occurrence : constant Exception_Occurrence;

   function Exception_Name (X : Exception_Occurrence) return String;
   --  Same as Exception_Name (Exception_Identity (X))

   function Exception_Name (Id : Exception_Id) return String;

   procedure Raise_Exception (E : in Exception_Id; Message : in String := "");

   function Exception_Message (X : Exception_Occurrence) return String;

   procedure Reraise_Occurrence (X : Exception_Occurrence);

   function Exception_Identity (X : Exception_Occurrence) return Exception_Id;

   function Exception_Information (X : Exception_Occurrence) return String;

   procedure Save_Occurrence
     (Target :    out Exception_Occurrence;
      Source : in     Exception_Occurrence);

   function Save_Occurrence
     (Source : in Exception_Occurrence)
      return Exception_Occurrence_Access;

   . . .
end Ada.Exceptions;
Each distinct exception is represented by a distinct value of type Exception_Id. Null_Id does not represent any exception, and is the default initial value of the type Exception_Id. Each occurrence of an exception is represented by a value of the type Exception_Occurrence. Similarly, Null_Occurrence does not represent any exception occurrence; it is the default initial value of type Exception_Occurrence.

next up previous contents index
Next: GNAT Implementation Up: Exceptions Previous: Exceptions   Contents   Index
(c) Javier Miranda. Canary Islands (Spain), 2002. Version 1.0