ON ERROR

ON ERROR GOTO [branchLabel]

Description:

Liberty BASIC 4 adds support for ON ERROR GOTO. Several of the QBasic error codes are supported, but some are not relevant, and there are some new ones. When an error occurs, the special variables Err and Err$ hold numeric and string values describing what sort of error happened. Some kinds of errors do not have a numeric value, in which case Err will be zero.

If an error occurs in a user function or subroutine, Liberty BASIC will exit the current function or subroutine and continue to exit functions and subroutines until it finds ON ERROR handler.

If an error is encountered, a program can attempt to resume execution with the RESUME statement.

Here is a short list of error codes:

  3 RETURN without GOSUB
  4 Read past end of data
  8 Branch label not found
  9 Subscript out of range
 11 Division by zero
 53 OS Error: The system cannot find the file specified.
 58 OS Error: Cannot create a file when that file already exists.
 55 Error opening file
 52 Bad file handle
 62 Input past end of file

Usage:

  on error goto [errorHandler]
  open "sillyfilename.txt" for input as #f
  close #f
  end

[errorHandler]
  print "Error string is " + chr$(34) + Err$ + chr$(34)
  print "Error number is ";Err
  end

The program above will print the following in the mainwin:

  Error string is "OS Error: The system cannot find the file specified."
  Error number is 53

Here an example that demonstrates the RESUME statement:

  on error goto [whoops]
  global divideBy
  call causeWhoops
  end

[whoops]
  print "whoops!"
  print "Error "; Err$; " "; " code "; Err
  divideBy = 2
  resume

sub causeWhoops
  print 10 / divideBy
end sub