FreeBasic Resume Next Keyword
last modified June 16, 2025
The FreeBasic Resume Next statement is used in error handling to
continue execution with the line immediately following the one that caused
the error. It works with the On Error statement to control
program flow when errors occur.
Basic Definition
Resume Next is part of FreeBasic's error handling mechanism.
When an error occurs, execution continues at the next line instead of
terminating the program.
This approach is useful when you want to ignore certain errors or when errors are non-critical to program execution. It allows the program to continue running despite encountering problems.
Simple Resume Next Example
This basic example shows how to use Resume Next to handle
division by zero errors.
On Error Resume Next
Dim a As Integer = 10
Dim b As Integer = 0
Dim result As Integer = a / b
If Err Then
Print "Error occurred: "; Err
Print "Error description: "; Error(Err)
Else
Print "Result: "; result
End If
Here we attempt division by zero, which would normally crash the program.
With Resume Next, execution continues and we can check the
Err variable to see if an error occurred. The error is
caught and handled gracefully.
Handling File Operations
Resume Next is particularly useful when dealing with file
operations that might fail.
On Error Resume Next
Open "nonexistent.txt" For Input As #1
If Err Then
Print "Could not open file: "; Error(Err)
Else
Print "File opened successfully"
Close #1
End If
This code attempts to open a non-existent file. Instead of crashing, the error is caught and an appropriate message is displayed. This makes the program more robust when dealing with external resources.
Multiple Operations with Error Checking
You can perform multiple operations and check for errors after each one.
On Error Resume Next
Dim values(5) As Integer
values(10) = 42 ' Out of bounds
If Err Then
Print "Array error: "; Error(Err)
Err = 0 ' Clear error
End If
Dim x As Integer = 100 / 0
If Err Then
Print "Math error: "; Error(Err)
Err = 0
End If
This example demonstrates handling multiple potential error sources. After each operation, we check for errors and clear the error flag before proceeding. This allows the program to continue despite multiple errors.
Nested Error Handling
You can nest error handling blocks with different On Error
statements.
On Error Resume Next
Print "Starting outer block"
Dim a As Integer = 10 / 0 ' Will be caught by outer handler
If Err Then
Print "Outer error: "; Error(Err)
Err = 0
On Error Goto 0 ' Disable error handling temporarily
Print "Starting inner block"
Dim b As Integer = 20 / 0 ' Will crash here
On Error Resume Next ' Re-enable error handling
End If
Print "Program continues"
This shows how to temporarily disable error handling within a block. The first error is caught, then we disable handling to let the second error crash the program. This technique can be useful for debugging.
Error Handling in Functions
Functions can use Resume Next to handle errors internally.
Function SafeDivide(a As Integer, b As Integer) As Integer
On Error Resume Next
SafeDivide = a / b
If Err Then SafeDivide = 0
End Function
Print "10 / 2 = "; SafeDivide(10, 2)
Print "10 / 0 = "; SafeDivide(10, 0)
The SafeDivide function handles division errors internally.
If division fails, it returns 0 instead of crashing. This makes the
function more robust when called with potentially invalid inputs.
Combining with Other Error Handlers
Resume Next can be combined with other error handling
techniques for more control.
Sub CriticalOperation()
On Error Goto ErrorHandler
Dim x As Integer = 100 / 0
Print "Operation succeeded"
Exit Sub
ErrorHandler:
Print "Critical error: "; Error(Err)
End
End Sub
On Error Resume Next
Print "Starting non-critical operations"
Dim y As Integer = 100 / 0 ' Will be ignored
Print "Continuing after error"
CriticalOperation()
This example shows different error handling approaches. Non-critical
operations use Resume Next, while critical operations
use a dedicated error handler. This provides flexibility in error
management.
Error Logging with Resume Next
You can use Resume Next to log errors while continuing
execution.
Sub LogError(errNum As Integer)
Open "errors.log" For Append As #1
Print #1, "Error "; errNum; ": "; Error(errNum); " at "; Time
Close #1
End Sub
On Error Resume Next
Dim values(5) As Integer
values(10) = 42 ' Out of bounds
If Err Then LogError(Err): Err = 0
Dim f As Double = Sqr(-1) ' Invalid argument
If Err Then LogError(Err): Err = 0
Print "Program completed (check errors.log for details)"
This code logs errors to a file while allowing the program to continue. Each error is recorded with a timestamp, providing an audit trail while maintaining program execution. This is useful for long-running programs.
Best Practices
- Specificity: Use
Resume Nextonly for non-critical errors. - Error Checking: Always check the
Errvariable after operations. - Error Clearing: Clear errors with
Err = 0after handling. - Logging: Consider logging ignored errors for debugging.
- Alternatives: For critical errors, use structured error handling instead.
This tutorial covered the FreeBasic Resume Next keyword with
practical examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.