Errors, Error Traps and Fixing Errors
This tutorial is about errors and their error codes,
error traps and fixing errors.
|
Error 1, NEXT without FOR:Possible Causes:
- There is a NEXT statement but the wrong variable name has been used.
Example:
FOR a = 0 to 10
NEXT b '<--- b should be a.- There are nested FOR and NEXT loops and there are not enough NEXT statements or variables for all FOR statements.
- There are nested FOR and NEXT loops and the variables following the NEXT statement(s) have been placed in the wrong order.
- An IF block been has used but there are not enough or no END IF statements or something else is wrong with the IF block.
- A SELECT CASE statement has been used but there are not enough or no END SELECT statements or something else is wrong.
Possible Solutions:
- Check the variable names.
- Place all the variables used by the FOR statements following a NEXT statement or following more than one NEXT statement in the reversed
order they appear in the FOR statements.- Check if the variables following the NEXT statements are placed
in the right order. Check the IF block and place an END IF statement if necessary.- Check if there are END SELECT statements where necessary and for other problems with SELECT CASE.
Example of Code Causing Problem:
'An example of variables 'placed in the wrong order. FOR a = 0 to 10 FOR b = 0 to 10 FOR c = 0 to 10 NEXT c NEXT a NEXT bExample of Right Code:
FOR a = 0 to 10 FOR b = 0 to 10 FOR c = 0 to 10 NEXT c NEXT b '<--- b should be located here and NEXT a '<--- a should be located over here.Error 2, Syntax error:Possible Causes:
- A keyword has been misspelled.
- A keyword is being used in a way
that is not allowed by Quick Basic.- In a call to a subroutine the name of
the subroutine has been misspelled.Possible Solutions:
- Check the spelling of keywords.
- Check the code for incorrect use
of keywords.- Check if the name of the subroutine
is spelled the same in the call to
the sub and the sub.Example of Code Causing Problem:
DECLARE SUB Test () Trst '<--- The name in the call has been misspelled.SUB Test END SUBExample of Right Code:
DECLARE SUB Test () TestSUB Test END SUBError 3, RETURN without GOSUBPossible Causes:
- Each time a GOSUB statement is used Quick Basic stores at which point in the program the GOSUB statement is located so when a RETURN statement is executed Quick Basic knows where to send the program to. When this error occurs there is no point to which Quick Basic can return and yet there is a RETURN statement telling it to return to somewhere in the program.
- If a CLEAR statement has been used, all information in the list described above will be removed, so if a CLEAR statement has been used in the program and a "RETURN without GOSUB" error occurs that most likely is the cause.
Possible Solutions:
- Make sure the program cannot run into a RETURN statement while
no GOSUB statement has been used before the program runs into the
RETURN statement.- Do not use CLEAR statements between GOSUB and RETURN statements.
Example of Code Causing Problem:
a = 0 DO GOSUB 1 LOOP UNTIL a = 101 a = a + 1 RETURNExample of Right Code:
a = 0 DO GOSUB 1 LOOP UNTIL a = 10 END '<--- At the end of the loop the program ' continues running and finds the RETURN ' statement while no GOSUB had been used ' before.1 a = a + 1 RETURNError 4, Out of data:Possible Causes:
A program tried to read more data
than available. This could have
the following causes:
- DATA items are not separated properly
because a comma was not added to
separate two DATA items.- An endless loop with a READ
statement has been used.- A condition to check whether the
end of the data is reached and
end the loop has an error in it.
Example:
DO READ Data$ LOOP UNTIL Data$ = "END"'UNTIL Data$ = "END"" is the condition 'in this example.
Possible Solutions:
- Check all DATA statements and data.
- Add a condition to the loop or use a FOR and NEXT loop.
- Check conditions.
If a loop has been set to run until a string
contains the text "END" it will not stop
if the string contains "end" so check for lower
case and upper case text or use LCASE$ and UCASE$.Example 1 of Code Causing Problem:
CLS RESTORE 1 FOR x = 0 to 2 READ n NEXT x1 DATA 1,2 3Example 1 of Right Code:
CLS RESTORE 1 FOR x = 0 to 2 READ n NEXT x1 DATA 1,2,3 'A comma has been added between 2 and 3.Example 2 of Code Causing Problem:
CLS RESTORE 1 DO READ a$ PRINT a$ LOOP END1 DATA January, February, March, April, May, June, July, August, September DATA October, November, DecemberExample 2 of Right Code:
CLS RESTORE 1 FOR r = 1 TO 12 READ a$ PRINT a$ NEXT r END1 DATA January, February, March, April, May, June, July, August, September DATA October, November, DecemberError 5, Illegal function call:Possible Causes:
(There are a lot of causes for this error, here some common causes are listed.)
- A row or column that does not exist has been specified
in a LOCATE statement.- A color index that does not exist has been specified
in a COLOR statement.- A graphics statement is used while in textmode.
- A frequency lower than 37 has been specified in a SOUND statement
(frequency zero is the only frequency below 37 that can be used.)- An instruction not supported by the PLAY or DRAW statement has
been used in a PLAY or DRAW statement.- The array used to store an image using a GET statement is too
small for the image.- The borders of a copied image would be located outside the view port's
borders using the coordinates specified in a PUT statement.- A negative value has specified in the functions LEFT$, RIGHT$, MID$
or a MID$ statement.Possible Solutions:
- Change the coordinates used.
- Use the WIDTH statement to change the number of rows and columns available.
- Use another screen mode.
- Use another color index or use another screen mode.
- Set a graphics mode before a graphics statement is executed.
- Use another frequency.
- Check the instructions for any misspelled instructions and check the instructions used and the values specified.
- Change the array's size.
- Change the coordinates used or change the height or width of the copied image.
- Use LEFT$ instead of RIGHT$ or RIGHT$ instead of LEFT$ with a positive value.
- Do not use a negative value in a MID$ function/statement.
Example 1 of Code Causing Problem:
CIRCLE (320, 240), 100Example 1 of Right Code:
SCREEN 12 '<--- A graphics mode had not been set before using the graphics statement CIRCLE. CIRCLE (320, 240), 100Error 6, Overflow:Possible Causes:
- The result of a calculation is too large.
- Trying to store a value in a variable that is too large for the variable.
- Using a value that is too large for a specific function.
Possible Solutions:
- Check the calculation for errors and change the data types of any variables that are used in the calculation.
- Change the variable's data type.
- Change the value used or use another function.
Example 1 of Code Causing Problem:
PRINT 30000 + 9000Example 1 of Right Code:
a = 30000 b = 9000 PRINT a + bExample 2 of Code Causing Problem:
DIM Test AS INTEGER Test = 38000Example 2 of Right Code:
DIM Test AS INTEGER Test = 38000
Example 3 of Code Causing Problem:
PRINT CINT(40000)
Example 3 of Right Code:
PRINT CLNG(40000)
Error 8, Label not defined:Possible Causes:
- The label or line number referred to does not exist.
- A label or line number is not in the same subroutine/function
as the statement referring to it (this does not count
for event trapping statements (ON ... GOSUB/GOTO.)- No colon was added to the label.
Possible Solutions:
- Check the spelling and line numbers
referred to and the line numbers
and labels. Add labels and line numbers
where necessary.- Check if labels and line numbers referred
to by ON ... GOSUB/GOTO statements
are in the program's main routine.
Check if labels and line numbers referred
to by GOTO, GOSUB, RESUME, RETURN and
RUN statements are in the same subroutine/function.- Check if all label names have a colon.
Example of Code Causing Problem:
GOTO Test END Tewst:Example of Right Code:
GOTO Test END Test: 'The label's name "Test" had been misspelled.Error 9, Subscript out of range:Possible Causes:
- An array has been made using DIM and the array is used in a subroutine/function but the array is not shared.
- A number higher than the upper bound or lower than the lower bound of one or more dimensions in the array is used.
- There is no array at all.
- The name of an array has been misspelled when creating it or referring to it.
- Trying not to make an array that is too large.
Possible Solutions:
- Make sure an array is shared.
- Calculations and loops work properly.
- There is an array.
- Check the references to the array and the DIM statement used to create the array.
- Try using '$DYNAMIC.
Example of Code Causing Problem:
'At the end of the loop 'n's value is 10 and in the 'code there is the following 'piece of code Number(n + 1), 'at the end of the loop that 'would mean show the 11th 'number of the array called Number 'while its upper bound is 10. DIM Number(10) CLS FOR n = 0 TO 10 STEP 2 PRINT Number(n), Number(n + 1) NEXT nExample of Right Code:
DIM Number(10) CLS FOR n = 0 TO 9 STEP 2 PRINT Number(n), Number(n + 1) NEXT n
Error 13, Type mismatch:Possible Causes:
- An attempt to convert a variable to another type such as when converting a string to a number.
- Using two variables of different types in a SWAP statement.
- A suffix was not added to a variable.
- The wrong suffix has been added to the variable.
- Two or more variables have been confused with each other.
- No quotes surrounding text that is being compared to or being stored in a string.
Possible Solutions:
- Use VAL to convert strings to numbers and STR$ to convert numbers to strings.
- Convert the variables to the same types using the STR$, VAL, CINT, CDBL, SNG or CLNG functions.
- Add the right suffix.
- Add the right suffix to the variable.
- Replace the variables used with the correct variables.
- Make sure the text is surrounded by quotes.
- This can also be done to solve the problem:
Check DEFINT, DEFDBL, DEFSNG, DEFLNG, DEFSTR, TYPE and DIM statements. Also check variable lists for subroutines and functions. Variables MUST be in the same order in a variable list of a DECLARE statement and a SUB/FUNCTION statement for the same subroutine/function.Example 1 of Code Causing Problem:
Number = Number$
Example 1 of Right Code:
Number = VAL(Number$)Example 2 of Code Causing Problem:
PRINT "Press the N key to continue." DO Key$ = INKEY$ LOOP UNTIL Key$ = NExample 2 of Right Code:
PRINT "Press the N key to continue." DO Key$ = INKEY$ LOOP UNTIL Key$ = "N"Error 14, Out of string space:Possible Causes:
- There is not enough memory for the strings used in the program.
Possible Solutions:
- Keep all strings as small as possible.
- Empty any strings that are not used.
- Change the size of the stack space.
Example of Code Causing Problem:
CLEAR , , 47000 Line$ = STRING$(2500, "-")Example of Right Code:
CLEAR , , 1000 '<--- The stack has been set to a smaller size. Line$ = STRING$(2500, "-")
Error -, Out of stack space:Possible Causes:
- A subroutine has been called to return to it from another subroutine.
- Too many nested subroutines.
Possible Solutions:
- Do not call subroutine A to return to it from subroutine B if subroutine A called subroutine B.When subroutine A calls subroutine B, the program will automatically return to subroutine A when sub B is exited.
- Do not nest too many subroutines.
Here is some information about the stack:
- The stack is a list.
- When a subroutine is called it is added to the stack.
- When a subroutine is exited (which happens when it is done or when an EXIT SUB/RETURN statement is executed) it is removed from the stack and the program will return to the previous subroutine in the stack.
- Too many subroutines in the stack will cause an out of stack space error.
Example of Code Causing Problem:
DECLARE SUB AddNumbers (a, b) DECLARE SUB EnterNumbers () CLSCALL EnterNumbersSUB AddNumbers (a, b) PRINT a; "+"; b; "="; a + b CALL EnterNumbers END SUBSUB EnterNumbers INPUT "Enter number 1: ", a INPUT "Enter number 2: ", b CALL AddNumbers(a, b) '<--- Subroutine AddNumbers is called ' while the program would 'automatically return to it as soon as 'Enternumbers is finished. END SUB
Example of Right Code:
DECLARE SUB AddNumbers (a, b) DECLARE SUB EnterNumbers () CLSCALL EnterNumbersSUB AddNumbers (a, b) PRINT a; "+"; b; "="; a + b END SUBSUB EnterNumbers DO INPUT "Enter number 1: ", a INPUT "Enter number 2: ", b CALL AddNumbers(a, b) LOOP END SUB
|
The ERDEV function returns an error code from the device where the error occurred.
The ERDEV$ function returns a string containing the device's name.Example1:
ON ERROR GOTO 1 CLS CHDIR "A:" 2 END'Display the device name and error code returned by the device. 1 PRINT ERDEV, ERDEV$ RESUME 2
|
The ERL function returns the last line number
in the program that was passed before the
error occurred.
ON ERROR GOTO 1 'Tell to go to line number 1 'when an error occurs. CLS 100 'Line number 100. LOCATE , 90 'Attempt to place the cursor at column 90.2 END 'Prevents program from going into error trap.1 PRINT "The error occurred at or next to line number: "; ERL RESUME 2 'Tell to go to line number 2.
|
The ERR function returns the error code of the last error that occurred.
ON ERROR GOTO ErrorTrap SCREEN 0: CLS LINE (0, 0)-(10, 10), 1, BF 'Attempt to draw a blue box. 'This will cause an "Illegal function 'call" error because no graphics mode 'is set while the program is running. END ErrorTrap: COLOR 7, 0 PRINT "Error: "; ERR RESUME Quit Quit: END |
|
The ERROR statement makes Quick Basic return an error with with the given error code. An error code is nothing more than a number to identify the error with. Each error message has its own error code.
ERROR 5 'Causes an "Illegal function call" message to appear.
-------------------------------------------------------------------------------- List of All Errors and Error Codes:1 NEXT without FOR 37 Argument-count mismatch 2 Syntax error 38 Array not defined 3 RETURN without GOSUB 40 Variable required 4 Out of DATA 50 FIELD-overflow 5 Illegal function call 51 Internal error 6 Overflow 52 Bad file or number 7 Out of memory 53 File not found 8 Label not defined 54 Bad file mode 9 Subscript out of range 55 File already open 10 Duplicate definition 56 FIELD statement active 11 Division by zero 57 Device I/O error 12 Illegal in direct mode 58 File already exists 13 Type mismatch 59 Bad record length 14 Out of string space 61 Disk full 16 String formula too complex 62 Input past end of file 17 Cannot continue 63 Bad record number 18 Function not defined 64 Bad file name 19 No RESUME 67 Too many files 20 RESUME without error 68 Device unavailable 24 Device time out 69 Communication-buffer overflow 25 Device fault 70 Permission denied 26 FOR without NEXT 71 Disk not ready 27 Out of paper 72 Disk-media error 29 WHILE without WEND 73 Advanced feature unavailable 30 WEND without WHILE 74 Rename across disks 33 Duplicate label 75 Path/file access error 35 Subprogram not defined 76 Path not found--------------------------------------------------------------------------------
|
Use the ON ERROR GOTO statement to tell a program
to go to a label or line number in a program when
an error occurs.Use the RESUME statement to let the program
return to the point where the error occurred
in the program.Use RESUME with a line number or label to move to
a certain label or line number in the program.Use RESUME NEXT to return to the point following
the point in the program where the error occurred.Example:
ON ERROR GOTO DiskError CLS CHDIR "a:" ENDDiskError: DO PRINT "There is a problem with drive a:." PRINT "1 = Retry 2 = Ignore 3 = Quit" DO Key$ = INKEY$ LOOP WHILE Key$ = "" LOOP UNTIL INSTR("123", Key$)CLS IF Key$ = "1" THEN RESUME IF Key$ = "2" THEN RESUME NEXT IF Key$ = "3" THEN SYSTEM