ON ERROR
Jump to navigation
Jump to search
![]() |
![]() |
![]() |
![]() |
Description
- ON ERROR goto [branchLabel] provides a way to handle the event if your program encounters an error. Err and Err$ hold the error code number and the description. See Err.
Syntax
- on error goto [errorHandler] - (cannot be a sub)
Hints
- Here is a link to QBasic error codes. Several of these are supported by JB. Some do not apply. Other error codes have been added.
- Here is a short list of common 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 52 Bad file handle 53 OS Error: The system cannot find the file specified. 55 Error opening file 58 OS Error: Cannot create a file when that file already exists. 62 Input past end of file
- Some errors do not have a numeric value and the value of Err will be zero.
- Some errors could not be trapped (for example, pressing Ctrl+Beak)
- In JB, error handler is local to the procedure.
- If an error occurs in a user function or subroutine, Just BASIC will exit the current function or subroutine and continue to exit functions and subroutines until it finds ON ERROR handler.
- In JB and RB, there is no support for RESUME.
- In JB, there is no way to turn Error handling off. (so having it local in a sub/function might be wise).
- In RB the handler must be in the main scope.
Example
An example trying to open a file that does not exist. The following will be printed to the mainwin:
Error string is "OS Error: The system cannot find the file specified." Error number is 53
on error goto [errorHandler] open "nofilename.txt" for input as #f close #f end [errorHandler] print "Error string is " + chr$(34) + Err$ + chr$(34) print "Error number is ";Err end
Useful Procedures
- Function that catches error on
- loadbmp bmpName$, fName$
- (because there are some BMPs JB cannot open)
- loadbmp bmpName$, fName$
function loadBmpErrChk(bmpName$, fName$) 'returns 1 on success, 0 on failure on error goto [errHandler] loadbmp bmpName$, fName$ loadBmpErrChk = 1 exit function [errHandler] loadBmpErrChk = 0 end function