Acorn Computers BBC Micro Model B w/8271 — Acorn BBC Micro Model B w/8271 User Guide Manual PDF

125 27 Error handling If the computer is unable to deal with a situation such as this: PRINT 3/0 then it will report the fact to you with an ‘error message’ and then stop, waiting for your next command >PRINT 3/0 If you are just playing at the keyboard this entry is not a problem – in fact one of the main virtues of BASIC is that it does try to give you an indication of why it is unable to proceed. However if you are writing a program for someone else to use, and you do not want them to be bothered with error messages then you must take the precautions to deal with every possible error that might arise.

Division by zero The major tool in error handling is the statement ON ERROR GOTO 5000 Once the computer has encountered an ON ERROR GOTO statement it will no longer report errors and stop – instead it will go to line 5000 (or wherever you have told it to go to). The statement ON ERROR OFF makes the computer handle errors normally again. The computer has an error number for every error it may encounter and you can use the error number to enable you to know what has gone wrong. The error number is stored in the variable ERR . The error number for an attempt to divide by zero is 18 for example.

(The 5000 is an example – it could GOTO any line number you like.) 10 ON ERROR GOTO 2000 20 PRINT "HELLO" 30 PRINT 3/0 40 PRINT "BYE" 50 END 2000 PRINT ERR >RUN HELLO 18 The computer also remembers the line at which it detected the error and this number is stored in the variable ERL .