Errors, Error Traps and Fixing Errors


This tutorial is about errors and their error codes,
error traps and fixing errors.

  • Common Errors, their Causes and Solutions:
Error 1, NEXT without FOR:

Possible Causes:

  1. 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.
  2. There are nested FOR and NEXT loops and there are not enough NEXT statements or variables for all FOR statements.
  3. There are nested FOR and NEXT loops and the variables following the NEXT statement(s) have been placed in the wrong order.
  4. 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.
  5. A SELECT CASE statement has been used but there are not enough or no END SELECT statements or something else is wrong.

Possible Solutions:

  1. Check the variable names.
  2. 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.
  3. 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.
  4. 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 b

Example 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:

  1. A keyword has been misspelled.
  2. A keyword is being used in a way
    that is not allowed by Quick Basic.
  3. In a call to a subroutine the name of
    the subroutine has been misspelled.

Possible Solutions:

  1. Check the spelling of keywords.
  2. Check the code for incorrect use
    of keywords.
  3. 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 SUB

Example of Right Code:

DECLARE SUB Test ()
Test
SUB Test
END SUB
Error 3, RETURN without GOSUB

Possible Causes:

  1. 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.
  2. 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:

  1. 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. 
  2. Do not use CLEAR statements between GOSUB and RETURN statements.

Example of Code Causing Problem:

a = 0
 DO
  GOSUB 1
 LOOP UNTIL a = 10
1 a = a + 1
RETURN

Example 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
RETURN
Error 4, Out of data:

Possible Causes:
A program tried to read more data
than available. This could have
the following causes:

  1. DATA items are not separated properly
    because a comma was not added to
    separate two DATA items.
  2. An endless loop with a READ
    statement has been used.
  3. 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:

  1. Check all DATA statements and data.
  2. Add a condition to the loop or use a FOR and NEXT loop.
  3. 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 x
1
DATA 1,2 3

Example 1 of Right Code:

CLS
RESTORE 1
 FOR x = 0 to 2
  READ n
 NEXT x
1
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
END
1
DATA January, February, March, April, May, June, July, August, September
DATA October, November, December

Example 2 of Right Code:

CLS
RESTORE 1
 FOR r = 1 TO 12
  READ a$
  PRINT a$
 NEXT r
END
1
DATA January, February, March, April, May, June, July, August, September
DATA October, November, December
 
Error 5, Illegal function call:

Possible Causes:
(There are a lot of causes for this error, here some common causes are listed.)

  1. A row or column that does not exist has been specified
    in a LOCATE statement.
  2. A color index that does not exist has been specified
    in a COLOR statement.
  3. A graphics statement is used while in textmode.
  4. A frequency lower than 37 has been specified in a SOUND statement
    (frequency zero is the only frequency below 37 that can be used.)
  5. An instruction not supported by the PLAY or DRAW statement has
    been used in a PLAY or DRAW statement.
  6. The array used to store an image using a GET statement is too
    small for the image.
  7. The borders of a copied image would be located outside the view port's
    borders using the coordinates specified in a PUT statement.
  8. A negative value has specified in the functions LEFT$, RIGHT$, MID$
    or a MID$ statement.

Possible Solutions:

  1. Change the coordinates used.
  2. Use the WIDTH statement to change the number of rows and columns available.
  3. Use another screen mode.
  4. Use another color index or use another screen mode.
  5. Set a graphics mode before a graphics statement is executed.
  6. Use another frequency.
  7. Check the instructions for any misspelled instructions and check the instructions used and the values specified.
  8. Change the array's size.
  9. Change the coordinates used or change the height or width of the copied image.
  10. Use LEFT$ instead of RIGHT$ or RIGHT$ instead of LEFT$ with a positive value.
  11. Do not use a negative value in a MID$ function/statement.

Example 1 of Code Causing Problem:

CIRCLE (320, 240), 100

Example 1 of Right Code:

SCREEN 12 '<--- A graphics mode had not been set before using the graphics statement CIRCLE.
CIRCLE (320, 240), 100
Error 6, Overflow:

Possible Causes:

  1. The result of a calculation is too large.
  2. Trying to store a value in a variable that is too large for the variable.
  3. Using a value that is too large for a specific function.

Possible Solutions:

  1. Check the calculation for errors and change the data types of any variables that are used in the calculation.
  2. Change the variable's data type.
  3. Change the value used or use another function.

Example 1 of Code Causing Problem:

PRINT 30000 + 9000

Example 1 of Right Code:

a = 30000
b = 9000
PRINT a + b

Example 2 of Code Causing Problem:

DIM Test AS INTEGER
Test = 38000

Example 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:

  1. The label or line number referred to does not exist.
  2. 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.)
  3. No colon was added to the label.

Possible Solutions:

  1. Check the spelling and line numbers
    referred to and the line numbers
    and labels. Add labels and line numbers
    where necessary.
  2. 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.
  3. 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:

  1. An array has been made using DIM and the array is used in a subroutine/function but the array is not shared.
  2. A number higher than the upper bound or lower than the lower bound of one or more dimensions in the array is used.
  3. There is no array at all.
  4. The name of an array has been misspelled when creating it or referring to it.
  5. Trying not to make an array that is too large.

Possible Solutions:

  1. Make sure an array is shared.
  2. Calculations and loops work properly.
  3. There is an array.
  4. Check the references to the array and the DIM statement used to create the array.
  5. 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 n

Example 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:

  1. An attempt to convert a variable to another type such as when converting a string to a number.
  2. Using two variables of different types in a SWAP statement.
  3. A suffix was not added to a variable.
  4. The wrong suffix has been added to the variable.
  5. Two or more variables have been confused with each other.
  6. No quotes surrounding text that is being compared to or being stored in a string.

Possible Solutions:

  1. Use VAL to convert strings to numbers and STR$ to convert numbers to strings.
  2. Convert the variables to the same types using the STR$, VAL, CINT, CDBL, SNG or CLNG functions.
  3. Add the right suffix.
  4. Add the right suffix to the variable.
  5. Replace the variables used with the correct variables.
  6. Make sure the text is surrounded by quotes.
  7. 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$ = N

Example 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:

  1. There is not enough memory for the strings used in the program.

Possible Solutions:

  1. Keep all strings as small as possible.
  2. Empty any strings that are not used.
  3. 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:

  1. A subroutine has been called to return to it from another subroutine.
  2. Too many nested subroutines.

Possible Solutions:

  1. 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.
  2. Do not nest too many subroutines.

Here is some information about the stack:

  1. The stack is a list.
  2. When a subroutine is called it is added to the stack.
  3. 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.
  4. 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 ()
CLS
CALL EnterNumbers
SUB AddNumbers (a, b)
PRINT a; "+"; b; "="; a + b
CALL EnterNumbers
END SUB
SUB 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 ()
CLS
CALL EnterNumbers
SUB AddNumbers (a, b)
PRINT a; "+"; b; "="; a + b
END SUB
SUB EnterNumbers
 DO
  INPUT "Enter number 1: ", a
  INPUT "Enter number 2: ", b
  CALL AddNumbers(a, b)
 LOOP
END SUB
  • ERDEV and ERDEV$

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
  • ERL

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.
  • ERR
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
 
  • ERROR
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
--------------------------------------------------------------------------------
  • ON ERROR GOTO, RESUME and RESUME NEXT

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:"
END
DiskError:
 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

Originally posted at http://www.qb4all.com