Tutorial - Error Trapping

Runtime Errors

There is a well known saying "if something can go wrong it will". This is not the whole story. Often you will find that even if it can't go wrong it still will.

For this reason the language has a mechanism for trapping runtime errors and allowing them to handled or reported.

Take the following program :-

Program()
	Number i;
    Begin
	For i From 1 To 10 Do
		Output i, 1 / ( 5 - i );		
	EndFor;
    End;

When 'i' reaches 5 the program will try to divide 1 by zero. This is not actually possible. So the system will report and error and the program will stop. The output to the screen will be as follows.

prog_error_1.grs

1, 0.250000
2, 0.333333
3, 0.500000
4, 1.000000
5, Program aborted with untrapped error:-
  Invoked by: System
  Class: DivByZero
  Source: prog_error_1.grs(20,15)
  Message: Division Error

All Memory Freed
Program failed with exit code 5 ( Untrapped system error )

prog_error_1.txt

Trapping Errors

In a number of cases there will not be a lot you can do about this, so the program can be left as it is. In other cases you can do something. I will start with a simple trap to illustrate the mechanism. Take the following program :-

Program()
	Number i;
    Begin
	For i From 1 To 10 Do
		Output i, 1 / ( 5 - i );		
	EndFor;
      OnError
	Output "An error has occurred so the program is exiting";
    End;

prog_error_2.grs

1, 0.250000
2, 0.333333
3, 0.500000
4, 1.000000
5, "An error has occurred so the program is exiting"
Program completed successfully
All Memory Freed

prog_error_2.txt

In this case the programs own error message is reported and the system itself reports the program as having completed successfully.

In the previous example the trap was placed at the end of the main program. The trap can also be placed at the end of any routine or at the end of any statement list within a compound statement - such as the body of the For loop.

Program()
	Number i;
    Begin
	For i From 1 To 10 Do
		Output i, 1 / ( 5 - i );
	    OnError
		Output "Not Valid";		
	EndFor;
      OnError
	Output "An error has occurred so the program is exiting";
    End;

prog_error_3.grs

1, 0.250000
2, 0.333333
3, 0.500000
4, 1.000000
5, "Not Valid"
6, -1.000000
7, -0.500000
8, -0.333333
9, -0.250000
10, -0.200000
Program completed successfully
All Memory Freed

prog_error_3.txt

In this example the error is trapped and handled within the body of the loop so the For can keep on running.

When there are nested compound statements the error propagates out until it is trapped. If an error occurs within a routine but is not trapped by that routine it will propagate out to the calling routine.

The OnError trap can be followed by any executable code. BUT this code must NOT generate any further errors otherwise the program will terminate and kittens will die.

Example

The program below gives an example of the sort of thing you can do when you trap an error. The error itself is trapped by a function.

prog_error_4.grs Output of prog_error_4.grs prog_error_4.png