FreeBasic Resume Keyword
last modified June 16, 2025
The FreeBasic Resume keyword is used in error handling to
continue execution after an error occurs. It works with On Error
to provide structured exception handling. This tutorial covers all aspects
of using Resume in FreeBasic programs.
Basic Definition
The Resume statement redirects execution after an error occurs.
It must be used within an error handling routine marked by On Error.
FreeBasic provides three forms: Resume, Resume Next,
and Resume label.
Error handling with Resume allows programs to recover from
unexpected situations gracefully. It's particularly useful for file operations,
memory allocation, and other error-prone tasks.
Simple Resume Example
This basic example shows how to use Resume to retry an operation.
On Error Goto ErrorHandler
Dim x As Integer = 0
Dim y As Integer = 10
Print "Result: "; y / x
Exit Sub
ErrorHandler:
Print "Error occurred: "; Err
x = 1
Resume
When division by zero occurs, execution jumps to the error handler. The
handler sets x to 1 and uses Resume to retry the division.
This demonstrates basic error recovery with Resume.
Resume Next Example
Resume Next continues execution with the statement following
the one that caused the error.
On Error Goto ErrorHandler
Dim a(5) As Integer
Dim i As Integer = 10
Print "Value: "; a(i) ' Will cause out of bounds error
Print "This line executes after Resume Next"
Exit Sub
ErrorHandler:
Print "Array access error occurred"
Resume Next
The array access error triggers the handler, which uses Resume Next
to skip the faulty line. Execution continues with the next print statement.
This is useful when you want to ignore non-critical errors.
Resume with Label Example
Resume label transfers control to a specific label after handling
an error.
On Error Goto ErrorHandler
Open "nonexistent.txt" For Input As #1
Print "File opened successfully"
Close #1
Exit Sub
ErrorHandler:
Print "File error: "; Err
Resume AfterOpen
AfterOpen:
Print "Continuing after file operation"
When the file open fails, the error handler prints a message. Resume AfterOpen
jumps to the labeled section, skipping the original file operations. This
provides precise control over program flow after errors.
Nested Error Handling
Error handlers can be nested, with inner handlers using Resume
to return to outer handlers.
On Error Goto OuterHandler
Print "Starting operation"
On Error Goto InnerHandler
Dim z As Integer = 0
Print "Result: "; 10 / z
Exit Sub
InnerHandler:
Print "Inner handler caught error: "; Err
Resume Next
OuterHandler:
Print "Outer handler caught error: "; Err
Resume AfterMath
AfterMath:
Print "Operation complete"
The division error is first caught by the inner handler, which uses
Resume Next. If the inner handler didn't exist, the outer
handler would catch it. This shows how to create layered error handling.
Resume in File Operations
Resume is particularly useful for robust file handling.
On Error Goto FileError
Dim filename As String = "data.txt"
Dim attempts As Integer = 0
RetryOpen:
Open filename For Input As #1
Print "File opened successfully"
Close #1
Exit Sub
FileError:
attempts += 1
If attempts < 3 Then
Print "Attempt "; attempts; " failed, retrying..."
Sleep 1000
Resume RetryOpen
Else
Print "Failed after "; attempts; " attempts"
Resume GiveUp
End If
GiveUp:
Print "Using default data instead"
This example attempts to open a file up to three times before giving up.
The Resume RetryOpen creates a retry loop, while Resume GiveUp
provides an alternative path when retries are exhausted.
Resume with Error Clearing
It's good practice to clear the error before resuming execution.
On Error Goto Handler
Dim p As Integer Ptr = 0
Print "Value: "; *p ' Null pointer dereference
Exit Sub
Handler:
Print "Memory error: "; Err
Err = 0 ' Clear the error
p = Allocate(SizeOf(Integer))
*p = 42
Resume
After handling the null pointer error, we clear the error with Err = 0,
allocate memory, and Resume to retry the operation. This prevents
the same error from being triggered repeatedly.
Resume in Function Calls
Resume can handle errors that occur in called functions.
Function Divide(a As Integer, b As Integer) As Integer
On Error Goto MathError
Return a / b
Exit Function
MathError:
Print "Division error in function"
Resume SafeReturn
SafeReturn:
Return 0
End Function
On Error Goto MainError
Print "Result: "; Divide(10, 0)
Exit Sub
MainError:
Print "Main error handler caught: "; Err
Resume Next
The function has its own error handler that uses Resume SafeReturn
to provide a default value. The main program's handler would catch any
unhandled errors from the function. This demonstrates layered error recovery.
Best Practices
- Clear errors: Always clear
Errbefore resuming. - Limit retries: Use counters to prevent infinite resume loops.
- Specific handlers: Create separate handlers for different error types.
- Document: Comment resume points for maintainability.
- Alternative paths: Provide safe alternatives when resuming isn't possible.
This tutorial covered the FreeBasic Resume keyword with practical
examples showing error handling and recovery techniques.
Author
List all FreeBasic Tutorials.