Filing system:
- Causes of error
- Classification of errors
- Signals and exceptions
Broadly we can identify three causes of data error:
- Domain/data errors: an operation is called with data that cannot be handled by that operation, e.g. divide by
zero, integer overflow, etc.
- 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.
- 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);
}
There are various methods whereby commonly experienced errors can be classified. These can be summarised as
follows.
- Synchronous/Asynchronous (Internal/ External):
- Synchronous (Internal) - Integer overflow, running out of memory.
- Asynchronous (External) - User interrupts, power failure.
- Reproducing/Nonreproducing:
- Reproducing - Same error will occur on each invocation, integer overflow.
- Nonreproducing - external errors, running out of memory.
- Errors usually result in the termination of a program, i.e. it "crashes".
- For many application such an uncontrolled termination is undesirable.
- Most programming languages leave the onus of dealing with errors with the programmer.
- There are two mechanisms whereby a program can regain control after an error has been detected.
- Signals.
- 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;
FILING: IN tray - return to imperative home page, OUT' tray - continue.
Created and maintained by
Frans Coenen.
Last updated 03 July 2001