ZetCode

FreeBasic Error Keyword

last modified June 16, 2025

The FreeBasic Error keyword is used for error handling and exception management. It allows programs to gracefully handle runtime errors and unexpected conditions.

Basic Definition

In FreeBasic, Error is used to raise custom errors or handle runtime exceptions. It works with On Error statements to create robust error handling routines.

The error handling mechanism helps prevent program crashes and allows for controlled recovery from exceptional situations. FreeBasic provides both built-in and user-defined error codes.

Simple Error Handling

This example demonstrates basic error handling with On Error.

error_basic.bas
On Error Goto ErrorHandler

Dim x As Integer = 10
Dim y As Integer = 0

Print "Result: "; x / y  ' This will cause division by zero error
Exit Sub

ErrorHandler:
Print "Error occurred: "; Err
Print "Error description: "; Error$(Err)
Resume Next

Here we set up an error handler that jumps to the ErrorHandler label when an error occurs. The division by zero triggers the handler, which prints the error code and description before continuing.

Raising Custom Errors

You can raise custom errors using the Error keyword.

error_custom.bas
Sub ProcessValue(n As Integer)
    If n < 0 Then
        Error 1000  ' Raise custom error
    End If
    Print "Processing value: "; n
End Sub

On Error Goto Handler

ProcessValue(10)
ProcessValue(-5)  ' This will trigger our custom error
Exit Sub

Handler:
Print "Custom error caught: "; Err
If Err = 1000 Then
    Print "Negative values not allowed"
End If
Resume Next

This code defines a custom error (1000) for negative input values. When the error occurs, control transfers to the handler which provides a specific message for our custom error code.

Error Resume Next

The Resume Next statement continues execution after an error.

error_resume.bas
On Error Resume Next

Dim a(5) As Integer
a(10) = 42  ' This would normally cause an out-of-bounds error

If Err Then
    Print "Array access error occurred"
    Err = 0  ' Clear the error
End If

Print "Program continues normally"

On Error Resume Next suppresses normal error handling and continues execution. We manually check Err to detect if an error occurred. This approach is useful for non-critical operations.

Nested Error Handlers

FreeBasic allows nesting error handlers for more complex scenarios.

error_nested.bas
Sub InnerSub()
    On Error Goto InnerHandler
    Error 200  ' Simulate an error
    Exit Sub
    
InnerHandler:
    Print "Inner handler caught error: "; Err
    Resume Next
End Sub

On Error Goto OuterHandler

Print "Calling InnerSub"
InnerSub()
Print "Back from InnerSub"
Exit Sub

OuterHandler:
Print "Outer handler caught error: "; Err
Resume Next

This example shows nested error handling. The inner handler catches and handles its error, so the outer handler never sees it. Each subroutine can have its own error handling logic.

Error Object

FreeBasic provides the Err object for error information.

error_object.bas
On Error Goto Handler

Open "nonexistent.txt" For Input As #1  ' This will fail
Exit Sub

Handler:
Print "Error number: "; Err
Print "Error description: "; Error$(Err)
Print "Error line: "; Erl
Resume Next

The Err object contains the error number, while Error$ provides the description. Erl gives the line number where the error occurred, helping with debugging.

Error Handling in Functions

Functions can use error handling to validate parameters or operations.

error_function.bas
Function SafeDivide(a As Integer, b As Integer) As Double
    On Error Goto DivError
    Return a / b
    Exit Function
    
DivError:
    Print "Division error in SafeDivide"
    Return 0
End Function

Print "10 / 2 = "; SafeDivide(10, 2)
Print "10 / 0 = "; SafeDivide(10, 0)

This function implements its own error handling for division operations. When division by zero occurs, it catches the error and returns 0 instead of crashing. The calling code remains clean and simple.

Disabling Error Handling

You can disable error handling with On Error Goto 0.

error_disable.bas
On Error Goto Handler

Print "Before critical section"
On Error Goto 0  ' Disable error handling

' Critical section where we want immediate failure on errors
Dim p As Integer Ptr = 0
*p = 42  ' This will crash immediately

Print "This won't be printed"
Exit Sub

Handler:
Print "This handler won't be reached for the critical error"
Resume Next

This code shows how to temporarily disable error handling. In critical sections, immediate failure might be preferable to error recovery. The null pointer assignment crashes the program as intended.

Best Practices

This tutorial covered the FreeBasic Error keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.