Data Structures and Algorithms
Ada Exceptions

Ada defines an EXCEPTION which may be processed in an exception handler at any level in the program above that where the exception was generated or RAISEd.

PACKAGE adtX IS
    TYPE X IS PRIVATE;
    EXCEPTION out_of_range;

    PROCEDURE f( a: INOUT X; b: INTEGER );
END adtX;

PACKAGE BODY adtX IS

    PROCEDURE f( a: INOUT X; b: INTEGER ) IS
        BEGIN
        ......
        IF b < some_limit THEN
            -- Normal processing
        ELSE
            RAISE out_of_range;
        END IF;
END adtX;
This package exports the exception out_of_range which may be caught in any routine that uses f.
WITH adtX; USE adtX;  -- Import adtX

PROCEDURE g( ... ) IS
    
    BEGIN
    ...
    f( a, n );   -- Invoke method f
    ...          -- Continue here if exception not raised

    ....         -- Return from here if no errors
    EXCEPTION
        WHEN out_of_range =>
            ...  -- process the exception
    END g;
In this example, the exception was processed in the procedure, g, which called the function, f, in which it was raised. The code processing the exception is any set of Ada statements: it could even raise another exception.

If the exception is not 'caught' it is propagated up the call stack until it encounters an exception handler prepared to process it. (If there are no exception handlers, then it will propagate to the highest level and cause the program to abort. However an implementation would be expected to print out the name of the exception causing the abort.)

Because they are propagated to arbitrarily high levels of an Ada program, it is easy to arrange for Ada exceptions to be caught at some level where there is an appropriate interface for dealing with them. For example, in a GUI program, the routines which handle interaction with a user through the windows, mouse events, keyboard input, etc, are generally at the highest level in the program. These routines "know" how to pop up the alert box that tells the user that a problem has occurred and force him or her to take some action to correct the problem. Alternatively, in an embedded processor, they would "know" to send a message via a communications channel to some master processor.

Lower level, re-usable code should be able to function correctly in any environment - GUI, text terminal, embedded system, etc. Ada's ability to propagate exceptions to a level at which the program knows sufficient about the environment to output the appropriate messages makes life simple for the writer of re-usable software. Exceptions are defined which correspond to all the errors that could occur. Re-usable code simply raises the exceptions. The users of the code then have the flexibility to decide when (ie at what level) to process the exceptions.

An added benefit of Ada's exception mechanism is that it provides a uniform method of handling errors. Left to their own devices, programmers are able to define a large grab-bag of styles of error raising and processing, for example, we can:

In Ada, a disciplined group of programmers will use Ada's in-built exception handling uniformly to propagate exceptions to some agreed level in programs where code which "knows" the current environment can handle the problem appropriately.

Ada further standardises behaviour by pre-defining a number of exceptions for commonly encountered problems, such as constraint_error when an attempt is made to assign a value to a variable is outside the permitted range for its type.

Key terms

Exception
An exception is raised by a program or program module when some event occurs which should be handled by some other program or module.

Continue on to C++ exception handling
Back to the Table of Contents
© John Morris, 1998