home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / funkybox.zip / GOBANANA.DOC < prev    next >
Text File  |  1983-04-01  |  12KB  |  236 lines

  1. ***********************
  2.   ON ERROR GO Bananas
  3. ***********************
  4.  
  5. PEOPLE SYSTEMS, LTD. -- SOFTWARE
  6. Copyright 1983 (c) L. A. Warner
  7.  
  8. A short tutorial on error trapping and debugging in BASIC. It could
  9. be a fat book if we tried to review the whole subject, but that is not
  10. the intention of this program. We will try to make some sense out 
  11. of the lack of priority in the manuals, and put some perspective
  12. on the subject, for those who are not too familiar with it. For a
  13. definition let us say that the purpose of ERROR handling routines is 
  14. to catch the error and provide a means of handling it and keep the
  15. program running, instead of shutting down when an error message is 
  16. displayed. This is the purpose of error trapping in professional
  17. software.
  18.  
  19. DEBUGGING  VS.  ERROR TRAPPING
  20. First, let us separate DEBUGGING from ERROR handling so that we can
  21. discuss them independently. Although there is a considerable
  22. overlap, DEBUGGING a program is quite different from trapping run-
  23. time errors in a well debugged program. Or saying it in another way,
  24. ERROR TRAPPING routines should be kept out of a program until the
  25. program has been well planned and executed, and thoroughly debugged.
  26. Don't trap errors if you can conveniently eliminate them.
  27.  
  28. All program proceedures should be designed to eliminate errors in
  29. keyboard inputs, and in peripheral operations, as well as the expected
  30. program integrity for its intended purposes. There are many examples
  31. in the manuals of how to extract the correct inputs and interfaces
  32. with peripherals. The more experience playing ~what if~ a pro-
  33. grammer has, the better he gets at DEBUGGING. We'll talk about 
  34. DEBUGGING in another article in the series.
  35.  
  36. LONG PROGRAMS
  37. Let us also eliminate the short programs that we play with for learn-
  38. ing purposes from our consideration. This article is aimed at the big
  39. program that takes 2 -3K or more of disk storage. The principles apply
  40. to the small ones, but their problems are much clearer and easier to
  41. isolate. In fact, one ON ERROR GOTO routine may be all that's needed
  42. for a short program. However, we will recommend ERROR
  43. trapping routines be sectionalized in various parts of  big pro-
  44. grams, instead of trying to write one humongous ON ERROR GOTO
  45. catch-all that can cause all kinds of interactive problems, that may
  46. unravel at the speed of light if stirred up, like a hornet's nest.
  47.  
  48. Let us assume we have a well debugged program, and we want to keep
  49. the possible peripheral and keyboard errors from causing an error
  50. message that shuts down the program and loses all the work.
  51. For example, one of the common errors to protect against 
  52. is the problem of keyboard inputs that name a file to OPEN it.
  53.  
  54. FILE NAME ERRORS
  55. Even with a protected input subroutine for keyboard (see VOL 2.0) an
  56. operator can enter a name that is correct but just one letter short and
  57. the CPU will issue an error message like File not found and shut down
  58. with it's quizzical ~OK~. And you know the users are going to sit there
  59. wondering what to do now. An error trapping routine is needed
  60. that lets the operator have another chance to ~TRY AGAIN~ while looking
  61. at the incorrect input string and if possible, the directory of FILES.
  62.  
  63. TRY AGAIN - VIEW THE PREVIOUS ERROR AND FILES ROUTINE
  64. A simple way to show FILES and encourage correct FILEname type-in.
  65.  
  66. 100 PRINT "PERHAPS YOU WOULD LIKE TO SEE THE FILEnames?"
  67. 110 PRINT "LOOK AT THE BOTTOM OF THE LIST FOR THE NAME"
  68. 120 PRINT
  69. 130 FILES  '**** prints out on screen list of FILEnames ****
  70. 140 PRINT
  71. 150 PRINT "NOW ENTER THE NAME CAREFULLY"
  72. 160 PRINT "TYPE IT THEN ENTER,  EXACTLY AS SHOWN"
  73. 170 PRINT
  74. 180 ON ERROR GOTO 1000   'error trap in place before input
  75. 190 GOSUB 20000              'controlled input routine ( see Vol 2.0 )
  76. 200 REM                      'returns WORD$
  77. 210 NAME1$ = WORD$           'assigns WORD$ to a $ and saves it
  78. 220 OPEN WORD$ FOR INPUT AS #1   'opens NAMEfile for input to program
  79. 230 FOR Z = 1 TO 15: INPUT #1, LK1$(Z),DLK1$(Z) : NEXT
  80. 240 PRINT    'line above reads disk into arrays LK1$, DLK1$
  81. 250 ON ERROR GOTO 0   'turns off the ERROR trapping
  82. 260 PRINT "GOOD SHOW -- THAT WAS FINE !" 'read-in completed
  83. *
  84. *
  85. *
  86. 999 GOTO XXXX  'this bypasses the ERROR routine following
  87. 1000 PRINT " PLEASE, TRY AGAIN ":BEEP : RESUME 170
  88. REM     'simple error routine returns to do it again - don't CLS
  89. *
  90.  
  91. STUDY THE ERROR MESSAGES
  92. Try this in BASIC-direct mode -  ENTER   ERROR n  -- see the messages.
  93. Of course, IBM-PC Error Messages are shown in Appendix A, but there 
  94. are some errors that are unprintable (31-49). Did you know, the pro-
  95. grammer can create his own error routines and messages by using num-
  96. bers above 200 (See ERROR n  --  page 4-80 in BASIC). IBM-PC's BASIC
  97. is very extensive and error trapping is well supported and elegant.
  98.  
  99. IBM-PC ROM IS WHERE IT'S AT.
  100. It's all in the 40 K of super ROM that demonstrates IBM's expertise.
  101. Although there have been few articles written on the subject,
  102. I'm sure you're aware that it's IBM's effective ROM and ERROR trapping
  103. and the extended BASIC that makes a programmer's life pleasant. Reviews
  104. comparing RAM sizes and drive capacities and plug-in slots don't
  105. mean much. But DOS, ROM and the elegant BASIC is where IBM-PC's at.
  106.  
  107. VARIOUS TYPES OF USERS
  108. There are at least three classes of users or operators that must be
  109. considered by the programmer. The highest class of user is the
  110. technical operator, someone of the quality and accuracy of a program-
  111. mer, who can operate the CPU and make sense out of an ERROR and 
  112. probably wouldn't make the same mistake twice. This is a special type
  113. and should not be expected except in high tech software programs. And
  114. the high tech will probably turn it over to his secretary after he gets
  115. tired of playing with it, anyway.
  116.  
  117. Next in line of decreasing qualification is the secretary who will be
  118. using the program regularly and will develop a facility for operating 
  119. it without too many problems, but she is always being interrupted by
  120. the phone and can not stay at the program for long periods, so she
  121. needs especially clear instructions and prompts at every step.
  122.  
  123. THE YOUNGSTER IN THE ARCADE
  124. Now, at the other end of the qualification spectrum we have the young
  125. game player, who will, when things get boring, test the programming to
  126. see how he can make it mess-up, if possible. Or the neophyte that 
  127. doesn't pay attention to instructions the first or the second or the 
  128. third time, but expects to be bailed out whenever he gets into trouble.
  129. Obviously, game debugging and ERROR planning is a specialty far beyond
  130. the purview of this short article. Fool-proof programming isn't too
  131. difficult but idiot proofing is almost impossible.
  132.  
  133. There may be many more types of users in between these extremes, but
  134. of course. The programmer would do well to consider the possible 
  135. nature and quality of the users and plan accordingly. Is the user
  136. under stress at an alarm system? Does the user understand a foreign
  137. language better? Is he young, inexperienced or just having a good
  138. time in an arcade or a tavern? Is it a sensitive subject or are the
  139. users especially sensitive to the subject matter at this time?
  140.  
  141. So, decide the quality of the expected user and pattern your error and
  142. debugging planning just a little bit more ~friendly~, if you like that
  143. word, and you'll probably have few problems. But assume all users are
  144. as careful and know as much about the program as you do, and you will
  145. doubtless answer many long distance calls from irate consumers of your
  146. products.
  147.  
  148. BANANA ROUTINES
  149. On this disk there are several short routines that demonstrate some 
  150. of the options in ERROR handling. They all have BANANA n  for a title.
  151. It would be useful to print these out and play with them for a while,
  152. to get familiar with the process. Then come back and read the following
  153. sections for further information.
  154.  
  155. Incidentally, having read them all thoroughly, one of the better 
  156. books on the IBM-PC is David Lien's  "Learning IBM Basic for the
  157. Personal Computer" .It has a good introduction to ERROR handling. There
  158. is also " A Guide to Programming -- IBM Personal Computer " by Bruce
  159. Presley, that is also well worth owning. If you value your time, own
  160. good reference books, and use them regularly.
  161.  
  162. TYPICAL ERROR HANDLING ROUTINE
  163. Lets look at a typical ON ERROR handling routine :
  164.                                 (added after debugging)
  165. 90 REM --- there may have been programming preceding this point.
  166. 95 REM --- that didn't require error trapping and handling.
  167. 99 REM
  168. 100 REM --- program section requiring error routine follows
  169. 110 ON ERROR GOTO 1000  'on a small program this might be line 1
  170. 120 REM --- program section being trapped starts here
  171. *
  172. 900 ON ERROR GOTO 0  ' turns off error trapping when not needed.
  173. 950 REM --- end of program section
  174. *
  175. 999 GOTO 1200 :REM ---- protects against run-thru and hang-up
  176. *
  177. 1000 IF ERR=25 OR ERR = 27 THEN PRINT "SORRY--PRINTER NOT READY":
  178.             PRINT "PLEASE --PRESS RETURN WHEN READY" :INPUT ""; Z$:
  179.             RESUME  0    ' resume at the statement causing error
  180. 1010 IF ERR=53 THEN PRINT "TRY THE NAME AGAIN, PLEASE":RESUME 170
  181. *
  182. 1200 ON ERROR GOTO 2000  'error trapping for the next program section
  183. 1210 REM --- programming begins again
  184.  
  185. SUMMARY
  186. Important factors that are worth repeating : ON ERROR GOTO line must
  187. precede the error occuring in the program. The error handling routines
  188. should be by-passed by GOTO or END avoiding infinite loops or crashes.
  189. Turn off error trapping when not needed. The requirement for error 
  190. trapping is determined during debugging. Don't trap an error, correct
  191. the programming to eliminate it, if possible. Keep the error messages
  192. simple and polite, of course. Show the error to the operator, so it
  193. can be corrected, run it by  so that the error can be observed.
  194.  
  195. Generally error routines will interfer with debugging if imposed early.
  196. So dont put them into complex programs until debugging has determined
  197. where they are required. For every good rule, there is usually also
  198. a good exception that " im - proves " the rule. Yes, sometimes 
  199. debugging is helped with the proper error routine to keep from losing
  200. input data from crashes. It might be better to SAVE it to a disk file
  201. instead of confusing debugging with error handling. But in a complex
  202. situation the use of ERL -- error line -- may assist you in debugging.
  203.  
  204. TYPES OF RESUME STATEMENTS
  205. Study where you want the RESUME statement to take the program.
  206. There are four options : omitting RESUME will restart the program at 
  207. the point the ON ERROR GOTO directed it. If there is no error handling 
  208. routine, it will continue from there. This may be useful for avoiding 
  209. a loop when the operator isn't familiar with the equipment. In fact a
  210. counter C=C+1 with an IF THEN -- RESUME can test whether the
  211. user can recover but after three tries the program proceeds,
  212. perhaps without the printer or other peripheral, if at all possible.
  213.  
  214. The RESUME ### line should be carefully selected. Are you setting up a 
  215. loop, can the operator recover, if not is there a counter and an escape
  216. route? Don't forget to initialize the counter or it wont work in
  217. between RUNs. Of yes, we ought to remind you that RUN ### will
  218. clear variables and arrays each time, but a GOTO will not, nor will it
  219. reset a counter without the proper instruction, like C=0 in the right
  220. place.
  221.  
  222. RESUME  or RESUME 0  returns the action to the statement that caused 
  223. error. Which says that in the meantime the error must have been cor-
  224. rected or else a hang-up loop will begin. Do not use RESUME[0].
  225. This unnecessary form RESUME[0] has scared lots of IBM-PC-ers away. It
  226. really wasn't intended to be used in that form ( [] contain optional
  227. data, remember). But IBM would have been better advised to omit [0].
  228.  
  229. RESUME NEXT is self explanatory, it will restart at the next STATEMENT
  230. after the one causing the error, not the next LINE, so watch multiple
  231. statement lines when using RESUME NEXT.
  232.  
  233. Happy Error Chasing -- You will find it's easy once you get into it.
  234. Read the section on debugging, note how they overlap.
  235.  
  236.