Broadly we can identify three causes of data error:
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; |
#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.
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.
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 |
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