Getting Started
Introduction
Sample Programs
QBasic IDEs
History
Advice
Tools
Mini-Tutorial
Tutorial
Code Snippets

Resources
Web Sites
More Tutorials
Vendors
Books
Magazines
NewsLetters
NewsGroups
Forums
User Groups
Talk Shows
Blogs

GBIC >> QBasic >> Tutorial >> Exceptions

QBasic Information Center Tutorials - Exceptions
These tutorials were written to help you get a quick, but thorough, understanding of QBasic - the scope of the language as well as it's specific capabilities.

Exception Functions Summary
When QBasic detects an error it stops execution unless the programmer has placed specific response commands in the program.

The following functions are available to monitor program execution and to respond to any errors (exceptions) that occur.

    • Trace Execution    
     tron, troff 
    • Device Identification    
     erdev, erdev$ 
    • Error Information    
     erl, err 
    • Error Simulation    
     error 
    • Error Trapping    
     on error...goto, resume 

Exception Function Reference
Here's a quick reference of the available exception functions, in alphabetical order.

  • erdev - returns error code from the last device that generated a critical error
        result% = erdev
        

  • erdev$ - returns the name of the device that generated the error
        result$ = erdev$
        

  • erl - returns line number where most error occurred
        result% = erl
        
    Note: if lines are not numbered, erl returns the closest line number before the line where the error occurred.

  • err - returns most recent run-time error
        result% = err
        

  • error - simulates an occurrence of an error (QBasic or user-defined)
        error 11          # divide by zero error
        error 57          # device i/o error
        

  • on error..goto - enables error tracking and direct program to branch to an error handling routine or to simply resume execution with the next statement
        On Error GoTo Handler    # goes to error-handling routine
        On Error Resume Next     # continues on line following the error
        
    If On Error is not used, any error will end the QBasic program.

  • resume -
        resume line    # continues at "line"
        resume next    # continues at statement following the 
                       # one that caused the error
        

  • troff - disables tracing of program statements
        troff
        

  • tron - enables tracing of program statements
        tron
        

    When tracing is on, QBasic highlights each line of the programs as it executes. If the line affects the screen, the screen is also flashed to show the result.

Run-Time Error Codes
Here is a list of the QBasic run-time error codes, as supplied by err.

     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 name 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 timeout             ¦  69      Comm-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      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

If you have any suggestions for additions to these tutorials, please let me know.