Rationale for Ada 2005

John Barnes
Contents   Index   References   Search   Previous   Next 

6.2 Exceptions

There are two minor improvements in this area.
One concerns the detection of a null exception occurrence which might be useful in a routine for analysing a log of exceptions. This is tricky because although a constant Null_Occurrence is declared in the package Ada.Exceptions, the type Exception_Occurrence is limited and no equality is provided. So the obvious test cannot be performed.
We can however apply the function Exception_Identity to a value of the type Exception_Occurrence and this returns the corresponding Exception_Id. Thus we could check to see whether a particular occurrence X was caused by Program_Error by writing 
if Exception_Identity(X) = Program_Error'Identity then
However, in Ada 95, applying Exception_Identity to the value Null_Occurrence raises Constraint_Error so we have to resort to a revolting trick such as declaring a function as follows
function Is_Null_Occurrence(X: Exception_Occurrence) return Boolean is
   Id: Exception_Id;
begin
   Id := Exception_Identity(X);
   return False;
exception
   when Constraint_Error => return True;
end Is_Null_Occurrence;
We can now write some general analysis routine as 
procedure Process_Ex(X: in Exception_Occurrence) is
begin
   if Is_Null_Occurrence(X) then    -- OK in Ada 95
      -- process the case of a null occurrence
   else
      -- process proper occurrences
   end if;
end Process_Ex;
But the detection of Constraint_Error in Is_Null_Occurrence is clearly bad practice since it would be all too easy to mask some other error by mistake. Accordingly, in Ada 2005, the behaviour of Exception_Identity is changed to return Null_Id when applied to Null_Occurrence. So we can now dispense with the dodgy function Is_Null_Occurrence and just write 
procedure Process_Ex(X: in Exception_Occurrence) is
begin
   if Exception_Identity(X) = Null_Id then    -- OK in Ada 2005
      -- process the case of a null occurrence
   else
      -- process proper occurrences
   end if;
end Process_Ex;
Beware that, technically, we now have an incompatibility between Ada 95 and Ada 2005 since the nasty function Is_Null_Occurrence will always return False in Ada 2005.
Observe that Constraint_Error is also raised if any of the three functions Exception_Name, Exception_Message, or Exception_Information are applied to the value Null_Occurrence so the similar behaviour with Exception_Identity in Ada 95 is perhaps understandable at first sight. However, it is believed that it was not the intention of the language designers but got in by mistake. Actually the change described here was later deemed to apply to Ada 95 as well (see Section 9.2 which covers a number of retrospective changes to Ada 95).
The other change in the exception area concerns the raise statement. It is now possible (optionally of course) to supply a message thus 
raise An_Error with "A message";
This is purely for convenience and is identical to writing 
Raise_Exception(An_Error'Identity, "A message");
There is no change to the form of raise statement without an exception which simply reraises an existing occurrence.
Note the difference between 
raise An_Error;    -- message is implementation defined
and 
raise An_Error with "";    -- message is null
In the first case a subsequent call of Exception_Message returns implementation defined information about the error whereas in the second case it simply returns the given message which in this example is a null string.
A minor change to the procedure Raise_Exception is that it now raises Constraint_Error if the identity parameter is Null_Id. This is explained further in Section 6.4 below.
There are also additional functions in the package Ada.Exceptions to return the name of an exception as a Wide_String or Wide_Wide_String. They have identifiers Wide_Exception_Name and Wide_Wide_Exception_Name and are overloaded to take a parameter of type Exception_Id or Exception_Occurrence. The lower bound of the strings returned by these functions and by the existing functions Exception_Name, Exception_Message and Exception_Information is 1 (Ada 95 forgot to state this for the existing functions). The reader will recall that similar additional functions (and forgetfulness) in the package Ada.Tags were mentioned in Section 2.6 of the chapter on the object-oriented model.

Contents   Index   References   Search   Previous   Next 
© 2005, 2006, 2007 John Barnes Informatics.
Sponsored in part by:
The Ada Resource Association and its member companies: ARA Members AdaCore Polyspace Technologies Praxis Critical Systems IBM Rational Sofcheck and   Ada-Europe:
Ada-Europe