User Generated Errors
There may be cases where you want the program to generate its own errors. In these cases use the following :-
Error etypeexpr , textexpr ;
The error type will be one of the EReportCat enumerate values. The text expression will be any expression the yields a text value.
Take the following program :-
Program() Number i; Begin For i From 1 To 10 Do If i == 5 Then Error Invalid, "Bad value within program - quitting"; EndIf; Output i, 1 / ( 5 - i ); EndFor; End;
Here the program has predicted the error and has decided to abort the run. The program could have trapped this error with an OnError trap as with a system error.
1, 0.250000 2, 0.333333 3, 0.500000 4, 1.000000 Program aborted with untrapped error:- Invoked by: Program Class: Invalid Source: prog_error_5.grs(17,4) Message: Bad value within program - quitting All Memory Freed Program failed with exit code 6 ( Untrapped program error )
The Status Structure
The system maintains a Status value of type TStatus that is updated when an error occurs - either a system generated or a program generated one - with the details of the error. This value will be treated like a global public constant accessible anywhere within your code.
The simplest thing to do is :-
Output Status;
It is also possible to check various fields and act accordingly.
One thing to note is that you can use the Error statement within an error handler and no kittens will die. The error will be trapped but the next surrounding error handler.
Example
This is a modified version of the previous example. It has a routine that can either generate a system error or a program generated error. The program generated error is rather arbitrary and is just there to illustrate the mechanism.
prog_error_6.grs