Building Applications with CamelForth

4. Dealing with OZ errors

Any application which allows any sort of input (via files or the keyboard), must be able to cope with errors that can occur due to the application being pre-empted by OZ.

The three errors that need to be dealt with are assigned to deferred Forth words of the same name; (RC_QUIT), (RC_ESC) and (RC_DRAW).


(RC_QUIT)

This error occurs when OZ wants the application to terminate (usually due to it being KILLed in the Index). This deferred word should carry out any housekeeping (closing files etc) before finishing with BYE. By default, it is set to BYE itself, which is adequate if you are not using files.


(RC_ESC)

This error only occurs when ESCape is pressed and escape detection is enabled, so you can avoid it altogether if you prefer. On the other hand, if you set it to generate an exception of some kind, your application can allow any of its processes to be gracefully aborted (provided you are trapping errors, of course!).


(RC_DRAW)

This error occurs whenever the screen becomes corrupted, and the application must attempt to redraw it. Applications like BBC BASIC and CamelForth cheat here by insisting that OZ preserves the screen if at all possible.

However, if you can write a word to re-draw your application's screen easily, you should do so and reduce the strain on the operating system. In any case, when the system is very low on resources, this error will be generated anyway, so you must have some way of dealing with it.


Puzzle Of The Pyramid

In our example application, we have disabled escape detection, so do not need to worry about (RC_ESC) (in any case, the default action of ESCAPE would only cause an exception that would be trapped by our top-level word, defined in the last section).

We don't have any files to close or other housekeeping to do, so the default action of (RC_QUIT) will be fine.

That leaves only (RC_DRAW). For now, we'll cheat like CamelForth, but also provide a reasonably effective routine that will re-draw the console window and redescribe the current location:

    : PYRAMID-DRAW
        CINIT                       \ re-draw the console window
        game-over @                 \ check if a game is in progress
        IF  ." Another go (Y/N)? "  \ if not, must be at this prompt
        ELSE  describe              \ otherwise describe location
        THEN ;

    ' PYRAMID-DRAW IS (RC_DRAW)     \ use as action for RC_DRAW errors


Next section: Menus and Commands

Previous section: The top-level definition


More on CamelForth

Other ROMs

Back to the Z88 home page