Concepts of Programming Languages

(Sean Pound) #1

642 Chapter 14 Exception Handling and Event Handling


else
Put("ERROR -- new grade: ");
Put(New_Grade);
Put(" is out of range");
New_Line;
end if;
end; -- end of the subscript range block
end loop;
-- Produce output
Put("Limits Frequency");
New_Line; New_Line;
for Index in 0..9 loop
Limit_1 := 10 * Index;
Limit_2 := Limit_1 + 9;
if Index = 9 then
Limit_2 := 100;
end if;
Put(Limit_1);
Put(Limit_2);
Put(Freq(Index + 1));
New_Line;
end loop; -- for Index in 0..9...
end Grade_Distribution;

Notice that the code to handle invalid input grades is in its own local block.
This allows the program to continue after such exceptions are handled, as
in our earlier example that reads values from the keyboard. The handler for
negative input is also in its own block. The reason for this block is to restrict
the scope of the handler for Constraint_Error when it is raised by negative
input.

14.2.6 Evaluation


As is the case in some other language constructs, Ada’s design of exception
handling represents something of a consensus, at least at the time of its design
(the late 1970s and early 1980s), of ideas on the subject. For some time, Ada
was the only widely used language that included exception handling.
There are several problems with Ada’s exception handling. One problem is
the propagation model, which allows exceptions to be propagated to an outer
scope in which the exception is not visible. Also, it is not always possible to
determine the origin of propagated exceptions.
Another problem is the inadequacy of exception handling for tasks. For
example, a task that raises an exception but does not handle it simply dies.
Finally, when support for object-oriented programming was added in Ada 95,
its exception handling was not extended to deal with the new constructs. For
example, when several objects of a class are created and used in a block and
Free download pdf