Microsoft KB Archive/39860

From BetaArchive Wiki
Knowledge Base


Cannot Nest ON ERROR GOTO Error-Trapping Routines in Basic

Article ID: 39860

Article Last Modified on 10/20/2003



APPLIES TO

  • Microsoft BASIC Professional Development System 7.1
  • Microsoft BASIC Professional Development System 7.0



This article was previously published under Q39860

SUMMARY

After control is passed to an error handler in a Microsoft QuickBasic program, additional errors cannot be trapped until a RESUME, RESUME label, or RESUME NEXT occurs. Errors that occur within the error handler itself will stop the program. This behavior is by design and applies to Microsoft QuickBasic versions 2.0, 2.01, 3.0, 4.0, 4.0b, and 4.5, and to Microsoft Basic Compiler versions 6.0 and 6.0b for MS-DOS and MS OS/2. (If Basic did not have this design feature, an error occurring within the error handler would cause a recursive loop which would quickly use up all stack space and stop the program.)

This information also applies to Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 for MS-DOS and MS OS/2 when using global or "module level" error trapping. However, note that you may use a global (module level) error handler to trap an error that occurs inside a local error handler invoked with an ON LOCAL ERROR statement (a feature available in Microsoft Basic PDS versions 7.0 and 7.1). Thus you can trap errors that occur within ON LOCAL ERROR handlers in Basic PDS.

MORE INFORMATION

Error-trapping routines perform best when they help notify you or the program of an error condition, allow for the error to be corrected, then RESUME back into the program. The code within the error handler must not cause any errors itself, or else the program will stop.

For example, the following error-trapping structure could be used when attempting to open a user-supplied filename. If an error occurred during execution of the TRAP routine, the error would not be trapped by the program, and the program would abort. This means that you should avoid placing code inside error handlers that is likely to cause errors (such as calculations that might overflow, or file input/output that might fail with a disk problem).

The following is a code example:

ON ERROR GOTO TRAP
10  INPUT "Enter a FileName: ";a$
20  OPEN a$ for input as #1
END
TRAP:
   ' ERROR 2   ' If you uncommented this line, the program would abort
               ' with the forced run-time error 2, "Syntax Error".
   IF ERL = 20 THEN
      RESUME 10
   ELSE
      Print "The error Number is: ";err
      RESUME NEXT
   END IF
                


Additional query words: QuickBas BasicCom 2.00 3.00 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10

Keywords: KB39860