University of Virginia, Department of Computer Science
CS655: Programming Languages
Spring 2000

Exception Mechanism of Ada
Li, Peixian (Rick)
18 March 2000

Exception Mechanism of Ada

Introduction

Exception mechanism is a very important part of a programming language. It is the key part to ensure a program will not crash when expect or unexpected exception happens. According to STEELMAN [3], Ada is designed with focus on reliability, which increases the importance of its exception mechanism.

What is an exception? Exception is an unusual situation, that may require special processing. when it occurs the exception is said to be signalled (or raised). Exception handler is the special processing. e.g., return maximum integer, ask user for advice, exit a loop, ignore it, terminate program after closing files, etc. 

Beside a language-defined exception handling mechanism, there are several ways to  handle exceptions [1]:

        pop = proc(s: stack) 
              % REQUIRES: s is not empty 
              % MODIFIES: s 
              % EFFECT: remove the top element of s 

     Problems: software less robust (insecure) and makes debugging harder. 
     However, sometimes this is useful for efficiency.  

        status := stream$getc(s,c) 
        if status = EXCEPTION 
           then % exceptional processing. 
                end 
           % normal processing 

     Problems: inefficient in normal case, no warning if users forget to check code (insecure), code for
     exceptional cases gets in the way (readability), and hard to nest functional interfaces properly and
     check for exceptions at proper times. 

     Problems: slightly less efficient in normal case hard to communicate other information about exception 

     Problems: doesn't help unless that procedure can alter flow of control 

Comparing to those ways above, langauge-defined exception mechanism is more expressive, more secure more efficient, but the problems is it makes the language more complex.

Overview

Ada has an exception handling mechanism for responding to unplanned error situations detected in declarations and statements during execution. The exception situations include errors detected by hardware, software errors detected during execution, error situations in built-in operations, and user defined exceptions. Exception identifiers have a scope.[2]

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.

One disadvantage is it is less secure because caller does not know about the entire interface of callee.

References

  1. Gary T. Leavens. Iowa State University. Iowa State University CS342 Lectures. 1990;
  2. John Morris. University of West Alabama. Data Structures and Algorithms- Ada exceptions. 1998;
  3. David A. Wheeler. Institute for Defense Analyses. Ada, C, C++, and Java vs. The Steelman. 1996;


CS 655 University of Virginia
CS 655: Programming Languages
cs655-staff@cs.virginia.edu
Last modified: Tue Jan 18 11:10:50 2000