Concepts of Programming Languages

(Sean Pound) #1

640 Chapter 14 Exception Handling and Event Handling


reference is made to a record field that is not present in a discriminated union,
and in many other situations.
In addition to the exceptions defined in Standard, other predefined pack-
ages define other exceptions. For example, Ada.Text_IO defines the End_Error
exception.
User-defined exceptions are defined with the following declaration form:
exception_name_list : exception
Such exceptions are treated exactly as predefined exceptions, except that they
must be raised explicitly.
There are default handlers for the predefined exceptions, all of which result
in program termination.
Exceptions are explicitly raised with the raise statement, which has the
general form
raise [exception_name]
The only place a raise statement can appear without naming an excep-
tion is within an exception handler. In that case, it reraises the same exception
that caused execution of the handler. This has the effect of propagating the
exception according to the propagation rules stated previously. A raise in an
exception handler is useful when one wishes to print an error message where
an exception is raised but handle the exception elsewhere.
An Ada pragma is a directive to the compiler. Certain run-time checks that
are parts of the built-in exceptions can be disabled in Ada programs by use of
the Suppress pragma, the simple form of which is
pragma Suppress(check_name)
where check_name is the name of a particular exception check. Examples of
such checks are given later in this chapter.
The Suppress pragma can appear only in declaration sections. When
it appears, the specified check may be suspended in the associated block or
program unit of which the declaration section is a part. Explicit raises are not
affected by Suppress. Although it is not required, most Ada compilers imple-
ment the Suppress pragma.
Examples of checks that can be suppressed are the following: Index_
Check and Range_Check specify two of the checks that are normally done
in an Ada program; Index_Check refers to array subscript range checking;
Range_Check refers to checking such things as the range of a value being
assigned to a subtype variable. If either Index_Check or Range_Check is
violated, Constraint_Error is raised. Division_Check and Overflow_
Check are suppressible checks associated with Numeric_Error. The follow-
ing pragma disables array subscript range checking:

pragma Suppress(Index_Check);

There is an option of Suppress that allows the named check to be further
restricted to particular objects, types, subtypes, and program units.
Free download pdf