|Data Structures and Algorithms|
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;
PROCEDURE f( a: INOUT X; b: INTEGER );
PACKAGE BODY adtX IS
PROCEDURE f( a: INOUT X; b: INTEGER ) IS
IF b < some_limit THEN
-- Normal processing
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
f( a, n ); -- Invoke method f
... -- Continue here if exception not raised
.... -- Return from here if no errors
WHEN out_of_range =>
... -- process the exception
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:
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.
Continue on to C++ exception handling|
Back to the Table of Contents