Error Handling
Handling runtime errors.
FreeBASIC can handle the errors in the following ways:

- By default the program does nothing with the errors - they are silently ignored and code continues. In this case code should process possible errors in the next line by using the `Err` function.
- If compiled with -e or -ex options, FreeBASIC uses QB-like error handling.
- Future OOP versions of FreeBASIC may have a java-like TRY..CATCH...FINALLY exception handler implemented.

NOTE: The following information is valid unless the error produces an OS General Protection Fault (for example if the program writes outside the process memory area). In these cases the OS will immediately stop the program and issue an error: nothing can avoid it from inside FreeBASIC.
Default error handling
The default FreeBASIC behavior is to set the ERR variable and continue.
`
Dim As Integer e
Open "xzxwz.zwz" For Input As #1
e = Err
Print e
Sleep
`
(The example program supposes there is no `xzxwz.zwz` file). The program does not stop; it sets the ERR variable and continues. The error can be processed in the next line.
Some IO functions such as `Open` and `Put #`... can be used in function form, returning an error number or zero if successful.
`
Print Open ("xzxwz.zwz" For Input As #1)
Sleep
`
QuickBASIC-like error handling
If the -e or -ex switch is used at compile time, the program is expected to have a QB-like error handler enabled. If no handler processes the error, the program stops with an error.
Notice: if QB-Like error handling is used, the programmer should be prepared to handle all error conditions.
`
'' Compile with QB (-lang qb) dialect
'$lang: "qb"
On Error Goto FAILED
Open "xzxwz.zwz" For Input As #1
On Error Goto 0
Sleep
End
FAILED:
Dim As Integer e = Err
Print e
Sleep
End
`
`On Error` sets an error handling routine which the program will jump to when an error is found. `On Error Goto 0` disables the error handling.
If an error handling routine is not set when an error occurs, the program will stop and send the console an error message.

    Aborting program due to runtime error 2 (file not found)

The error handler routine can be at the end of the program, as in QB. The `On Local Error` statement allows the setting of a local error handler routine at the end of the same `Sub` or `Function` in which the error occurs.
`
'' Compile with -e
'' The -e command line option is needed to enable error handling.
Declare Sub foo
foo
Sleep
Sub foo
Dim filename As String
Dim errmsg As String
filename = ""
On Local Error Goto fail
Open filename For Input Access Read As #1
Print "No error"
On Local Error Goto 0
Exit Sub
fail:
errmsg = "Error " & Err & _
" in function " & *Erfn & _
" on line " & Erl
Print errmsg
End Sub
`
If the -e switch is used (whatever the -lang dialect), the error handler must terminate the program.
With -ex and -lang qb dialect only, the error routine can end by using `Resume` (retries the statement that caused the error) or `Resume Next` (continues at the next instruction) .
Error codes
See Runtime Error Codes for a listing of runtime error numbers and their associated meaning.
No user error code range is defined. If `Error` is used to set an error code it is wise to use high values to avoid collisions with the list of built-in error codes. (This built-in list may be expanded later.)
See also

- Error Handling Functions
- Runtime Error Codes