Disk Errors

Disk and I/O errors are always trapped at run-time by PowerBASIC.  If a run-time Disk or I/O error is detected, the error code is placed in the ERR system variable.  If ON ERROR is enabled, code execution will branch to the designated local error handler.

All error handling in PowerBASIC is local to each Sub or Function.  You cannot create a global error handler routine as you can in some DOS BASICs.

When an error occurs in PowerBASIC, an error code is placed into the ERR system variable.  If Error Trapping has been enabled, execution branches to the error trap.  Otherwise, execution continues.  If an error occurs and your code does not take care of it, either by using an error trap or by explicitly testing the ERR or ERRCLEAR variables, your program may produce unpredictable results.  For example, in the following code, several problems can occur which would cause the code to fail, and possibly even trigger a General Protection Fault (GPF) in Windows:

SUB ReadFile(Filnam$, buffer$(), Lines%)

  RESET Lines%

  OPEN Filnam$ FOR INPUT AS #1

  WHILE ISFALSE EOF(1)

    INCR Lines%

    LINE INPUT #1, buffer$(Lines%)

  WEND

  CLOSE #1

END SUB

Here, the ERR variable is not checked after the OPEN statement to see if it was successful.  If the file does not exist or has been locked by another process, a run-time error can occur.  In this case, EOF(1) will never be able to return TRUE (non-zero) since the file was not able to be opened, and therefore the end of the file cannot be determined.  Further, checking the EOF of a file that has not been opened will trigger yet another run-time error.

The result is that without adequate error testing, this small loop will begin to run continuously.

While certainly a flaw in the code, no harm will come to the program for period.  However, a fatal error in the LINE INPUT# statement is imminent if the Lines% variable value exceeds the UBOUND of the buffer$() array.  A fatal error could also occur if buffer$() was not previously dimensioned, or it wasn't dimensioned with enough elements to store the entire file (that is, assuming the file was opened successfully).

In these cases, a General Protection Fault (GPF) is quite likely to occur, as soon as invalid memory addresses begin to be accessed in an attempt to store the string data.  You can prevent the array boundary GPF by turning on error checking using the #DEBUG ERROR ON metastatement.  However, if the array was not previously dimensioned or does not have enough space, the code will still fail in its overall objective.

A more robust version of this example code follows:

#DEBUG ERROR ON

SUB ReadFile(Filnam$, buffer$(), Lines%)

  LOCAL Temp$

  ON ERROR RESUME NEXT

 

  RESET Lines%

  OPEN Filnam$ FOR INPUT AS #1

  IF ERR THEN                        'error opening file

    EXIT SUB

  END IF

 

  WHILE ISFALSE EOF(1)

    INCR Lines%

    LINE INPUT #1, Temp$

    IF ERR then EXIT SUB             'abort if disk error

    buffer$(Lines%) = Temp$

    IF ERR = 9 THEN                  'subscript out of range

      REDIM PRESERVE buffer$(Lines%) 'increase array size

      buffer$(Lines%) = Temp$

    END IF

  WEND

  CLOSE #1

END SUB

 

See Also

Error Trapping

Error Overview

Compile-time errors

Run-time errors