Concepts of Programming Languages

(Sean Pound) #1

638 Chapter 14 Exception Handling and Event Handling


continues, if necessary, to the main procedure, which is the dynamic root of
every Ada program. If an exception is propagated to the main procedure and a
handler is still not found, the program is terminated.
In the realm of exception handling, an Ada block is considered to be a
parameterless procedure that is “called” by its parent block when execution con-
trol reaches the block’s first statement. When an exception is raised in a block,
in either its declarations or executable statements, and the block has no handler
for it, the exception is propagated to the next larger enclosing static scope, which
is the code that “called” it. The point to which the exception is propagated is
just after the end of the block in which it occurred, which is its “return” point.
When an exception is raised in a package body and the package body
has no handler for the exception, the exception is propagated to the declara-
tion section of the unit containing the package declaration. If the package
happens to be a library unit (which is separately compiled), the program is
terminated.
If an exception occurs at the outermost level in a task body (not in a nested
block) and the task contains a handler for the exception, that handler is exe-
cuted and the task is marked as being completed. If the task does not have a
handler for the exception, the task is simply marked as being completed; the
exception is not propagated. The control mechanism of a task is too complex
to lend itself to a reasonable and simple answer to the question of where its
unhandled exceptions should be propagated.
Exceptions can also occur during the elaboration of the declarative sec-
tions of subprograms, blocks, packages, and tasks. When such exceptions
are raised in procedures, packages, and blocks, they are propagated exactly
as if the exception were raised in the associated code section. In the case of
a task, the task is marked as being completed, no further elaboration takes
place, and the built-in exception Tasking_Error is raised at the point of
activation for the task.

14.2.3 Continuation


In Ada, the block or unit that raises an exception, along with all units to which
the exception was propagated but that did not handle it, is always terminated.
Control never returns implicitly to the raising block or unit after the exception
is handled. Control simply continues after the exception clause, which is always
at the end of a block or unit. This causes an immediate return to a higher level
of control.
When deciding where execution would continue after exception handler
execution was completed in a program unit, the Ada design team had little
choice, because the requirements specification for Ada (Department of
Defense, 1980a) clearly states that program units that raise exceptions cannot
be continued or resumed. However, in the case of a block, a statement can be
retried after it raises an exception and that exception is handled. For example,
suppose a statement that can raise an exception and a handler for that exception
are both enclosed in a block, which is itself enclosed in a loop. The following
Free download pdf