GWBasic Graphics


ERROR TRAP


We have seen examples of the displays that we can create with CGA, EGA (Color Graphics and Extended Graphics) by using several Screen Modes. The same GWBasic code is used to create a specific plot, but the resulting display is quite different. The more evolved graphics are much nicer to view. It would be nice to make a plot and see how it looks in the different Screen Modes. This is what this next simple program will do. The program will create the same pattern in all the different modes available to our station.

Another useful tool is the GWBasic error handling capabilities. When dealing with graphics, sometimes an error is rather difficult to chase down because of the display mode being used. The normal text error messages might not be what we would expect when using SCREEN 0 text mode.

This program attempts to create the same display in all screen modes. Of course some modes will create an illegal operation error message. This can be controlled by creating our own error handling routine. As in previous examples, make a program file using the program given. A step by step explaination will be given as well.

Just a reminder that these exercises may be done on a floppy disk. This has the distinct advantage of keeping all the incidental files created in a location where they can be dealt with later. Of course, the exercise may be done on the hard drive, but if some files get misplaces, they may be hard to re-locate for subsequent deletion after the exercise.


A Graphic example  of the different Screen Modes 
This program will create a simple text message, identifying the Screen Mode being used
  a straight diagonal line and a circle in any of the screen modes that are available.


Copy and paste this program to be run with GWBasic......


10 '       _____ Prog____ERROR.BAS ______
20 ' this  module performs a sub-routine 20 times. The time through this loop
30 '   corresponds with the screen argument number used in the sub-routine.
40 '      Numeric variable MODE holds the number used for both events.
50 '
60    FOR MODE=1 TO 12: GOSUB 110: NEXT MODE
70      PRINT "program terminated "
80        END
90 '
100 '      ______ Sub Routine _________
110            ON ERROR GOTO 230
120               SCREEN MODE
130       PRINT SPACE$ (20), "Screen Mode "; MODE
140   FOR N= 0 TO 200: PSET (N,N): NEXT N
150   FOR N= 1 TO 360: PSET (98*COS(N)+101,98*SIN(N)+101): NEXT N
160              ON ERROR GOTO 0
170 '
180        N$= INKEY$: IF N$="" THEN 180
190             SCREEN 2: SCREEN 0
200                   RETURN
210 '
220 '
230 '      _____ Error Handler_________
240 '
250        RESUME 190
260 '
270 '
280 '
290   END '   ______handy back-up statement_______
300   SAVE "ERROR",A







Brief modular explaination for the program


The FOR/NEXT statement executes the sub-routine a given number of times, using the number in the variable (MODE) for both the FOR/NEXT loop and for the MODE for the GRAPHICS display. The plot will be displayed for the mode number in variable MODE using the Sub Routine until the FOR/NEXT loop is exhausted.

The first step in the Sub routine, enables our own error handler to handle errors instead of using GWBasic's normal error handler. Our error handler is deffined in the ON ERROR GOTO the line number statement. It is enabled at the beginning of the Sub-routine because it is the sub routine that we wish to test. GWBasic's error handler is restored with the ON ERROR GOTO 0 statement at the end of the Sub routine's current Screen's plot.

The remainder of the Sub routine halts the program until a key is pressed, giving us the opertunity to view the plot. The Screen mode is returned to SCREEN 0 via SCREEN 2. The RETURN statement returns from the Sub routine to the FOR/NEXT loop where MODE is incremented and the loop is repeated.

The Error handler simply sends program control to the part of the Sub routine that restores SCREEN 0, effectively circumventing the instruction for creating a plot.

The final program should display all the valid plots, halt for observation of the plot and finally providing a terminating message and quit.





Some notes:

Your borwser may display a funny symbol at the end of the program to be copied. Its a result of the control Z that GWBasic places at the end of a text file, is it will be in the Windows program file. It is no problem whether it is there or not. Windows knows the difference.






See the results of having no error handler in the program

To demostrate what happens with GWBasic's error handler, lets run our program with BWBasic's handler. This is easily done by putting a ' mark between the Line Number and the ON ERROR GOTO line Number effectivel making it a remark statement. Now run the program. Screen 1,2 and 3 function as expected. However when MODE= 4, an error is generated by GWBasic's error handling routine giving us the Illegal function call message.


Restore the ON ERROR GOTO as a program step by removing the ' so it no longer is a remark statement.
Run the program again to ensure that it works as we originally coded it.



Now put the error handler to more effective for developing a program:

Replace the remark line at line 240 with the folloing statement.
240 PRINT "error mode ";MODE
We won't see this message unless we halt the program long enough to see it. We can do this by changing the RESUME statement to the halt code in the sub routine by changing line 250 to:
250        RESUME 180
This will permit us a print out if we enter the error handler.

Make the changes in line 240 and 250 and see the results.





Now lets get a little more sophisticated.

Two of the built in variable in GWBasic are ERR and ERL. They can be read like any other variable. ERR provides the error code of the error and ERL provides the line number that the error was discovered. They can be included in the error message. Add line 241 and line 242 to the error handler.

230 '      _____ Error Handler
240 PRINT "error mode ";MOD
241 PRINT "error  code= ";ERR
242 PRINT "line number= ";ERL
250        RESUME 180



Another software Hook for developement

You may wish to periodically see how the program runs without seeing the error message. Use a variable with a name like TEST to contain a software switch. Put it at the first line of the program so its easy to change. If its set to zero at program RUN time, or any other number. If its set to any number other than zero, then the program will not branch at line number 231. If it is set to zero, then the error message will not be posted during program RUN time. Line 5 can be changed easily at any time during development.

5 TEST= 1
231 IF TEST= 0 then RESUME 190







Summary


This program example demonstrates how a program that is known to generate an error message may ignore the error byusing our own error handler. Our own error handler may also be used as an effective development tool during the creation of a program. A program may temperarily use the error handler to learn the behavior of the program during developement.


When you are satisfied with the program, all the cute little software hooks can be deleted so the program looks like the original program example. Or, you just may wish to leave all the hooks intact in case you want to make some changes in the program in the future. Thats one advantage of doing your own programming. You can change the program as often as you change your mind.

In short.... play around and have fun.........