ERROR HANDLING

CONTENTS:

  1. Causes of error.
  2. Classification of errors.
  3. Signals and exceptions.



1. CAUSES OF ERRORS

Broadly we can identify three causes of data error:

  1. Domain/data errors: an operation is called with data that cannot be handled by that operation, e.g. divide by zero, integer overflow, etc.
  2. Resource exhaustion: memory errors. Examples:
    • The Ada allocator new will fail if not enough memory is available.
    • The C allocator malloc will return NULL if not enough memory is available.
  3. Loss of facilities: network failure, power failure, etc.

Ada Divide by Zero Example:

with CS_IO ; use CS_IO ;

procedure ADA_ERROR_EXAMPLE is
        NUM_X, NUM_Y, NUM_Z: integer;
begin
        get(NUM_X);
        get(NUM_Y);
        NUM_Z := NUM_X/NUM_Y;
        put("Result is");
        put(NUM_Z);
        new_line;
end ADA_ERROR_EXAMPLE;

C Divide by Zero Example:

#include <stdio.h>

void main(void)
{
int num_x, num_y, num_z;

scanf("%d %d",&num_x,&num_y);

num_z = num_x/num_y;

printf("Result is %d\n",num_z);
}



2. CLASSIFICATION OF ERRORS

There are various methods whereby commonly experienced errors can be classified. These can be summarised as follows.




3. SIGNALS AND EXCEPTIONS


Signal Statements

A signal statement names an error condition and a procedure. When the error condition occurs the procedure is called. An error condition is a class of error, e.g. NUMERIC_ERROR (Ada). Signal statements can be implemented as a library routine. Disadvantage of signal statements is that they have no access to local variables.


Exception Handler

Under certain circumstances the execution of a statement may be unable to terminate as we would normally expect. The execution is then said to result in failure. If a routine executes a statement and that statement fails, this will prevent the routine's execution from proceeding as planned; such an exit is called an exceptiion.

An exception handler comprises an error condition and a set of statements that can be attached to a block of code. If a statement in the block fails with the error condition specified in the exception handler, the set of statements in the exception handler will be executed instead of the statements in the block. Ada has five predefined error conditions:

constraint_error
 
numeric_error
 
program_error
storage_error
 
 
tasking_error

Example Ada Exception handler

with CS_IO; use CS_IO;

procedure ADA_ERROR_HANDLER is
        NUM_X, NUM_Y, NUM_Z: integer;
begin
        get(NUM_X);
        get(NUM_Y);
        NUM_Z := NUM_X/NUM_Y;
        put("Result is");
        put(NUM_Z);
        new_line;
exception
        when NUMERIC_ERROR =>
                put_line("NUMERIC_ERROR");
                put("Exception handler ");
                put_line("has control");
                put("NUM_X = ");put(NUM_X);new_line;
                put("NUM_Y = ");put(NUM_Y);new_line;
                put("NUM_Z = ");put(NUM_Z);new_line;
end ADA_ERROR_HANDLER;

Note: Ada also allows user defined error conditions. Example:

TIME_UP: exception;



Return to imperative home page or continue.




Created and maintained by Frans Coenen. Last updated 03 July 2001