ZetCode

FreeBasic Exception Handling: Try/Catch/Throw

last modified June 16, 2025

The FreeBasic Try, Catch, and Throw keywords provide structured exception handling. They help manage runtime errors gracefully.

Basic Definition

Try defines a block of code where exceptions might occur. Catch handles exceptions thrown in the Try block. Throw raises an exception explicitly.

Exception handling separates error handling from normal program flow. It makes code more robust and maintainable by centralizing error management.

Simple Try/Catch Example

This basic example demonstrates the fundamental structure of exception handling.

simple_try_catch.bas
Try
    Print "Inside Try block"
    Throw "An error occurred"
    Print "This won't execute"
Catch (e As String)
    Print "Caught exception: "; e
End Try

The code throws a string exception which is caught by the Catch block. The second Print statement never executes because control transfers to Catch.

Catching Different Exception Types

FreeBasic allows catching different exception types with multiple Catch blocks.

multiple_catch.bas
Try
    Dim choice As Integer = 2
    Select Case choice
        Case 1: Throw "String exception"
        Case 2: Throw 42
        Case 3: Throw 3.14
    End Select
Catch (e As String)
    Print "Caught string: "; e
Catch (e As Integer)
    Print "Caught integer: "; e
Catch (e As Double)
    Print "Caught double: "; e
End Try

This example shows how to handle different exception types. The appropriate Catch block executes based on the thrown exception's type.

Nested Try/Catch Blocks

Try/Catch blocks can be nested to handle exceptions at different levels.

nested_try_catch.bas
Try
    Try
        Throw "Inner exception"
    Catch (e As String)
        Print "Inner catch: "; e
        Throw "Outer exception"  ' Re-throw
    End Try
Catch (e As String)
    Print "Outer catch: "; e
End Try

The inner Catch handles the first exception, then throws a new one. The outer Catch handles this second exception, demonstrating exception propagation.

Exception Handling in Functions

Functions can throw exceptions that are caught by the calling code.

function_exception.bas
Function Divide(a As Integer, b As Integer) As Double
    If b = 0 Then
        Throw "Division by zero"
    End If
    Return a / b
End Function

Try
    Print Divide(10, 2)
    Print Divide(5, 0)
Catch (e As String)
    Print "Error: "; e
End Try

The Divide function throws an exception for division by zero. The calling code catches and handles this exception, preventing program termination.

Custom Exception Objects

You can create custom types for more sophisticated exception handling.

custom_exception.bas
Type MyException
    Message As String
    ErrorCode As Integer
End Type

Try
    Dim ex As MyException
    ex.Message = "Custom error"
    ex.ErrorCode = 1001
    Throw ex
Catch (e As MyException)
    Print "Custom exception caught:"
    Print "Message: "; e.Message
    Print "Code: "; e.ErrorCode
End Try

This example defines a custom exception type with additional information. The Catch block accesses these properties for more detailed error handling.

Finally Block

FreeBasic supports Finally blocks for cleanup code that always executes.

finally_block.bas
Dim fileNum As Integer

Try
    fileNum = FreeFile()
    Open "test.txt" For Input As #fileNum
    ' File operations here
    Throw "Simulated error"
Catch (e As String)
    Print "Error: "; e
Finally
    If fileNum > 0 Then
        Close #fileNum
        Print "File closed in Finally block"
    End If
End Try

The Finally block ensures the file is closed whether an exception occurs or not. This is crucial for resource management and preventing leaks.

Rethrowing Exceptions

Sometimes you need to catch an exception, perform some handling, then rethrow it.

rethrow_exception.bas
Sub ProcessData()
    Try
        Throw "Data processing failed"
    Catch (e As String)
        Print "Logging error: "; e
        Throw  ' Re-throw the same exception
    End Try
End Sub

Try
    ProcessData()
Catch (e As String)
    Print "Caught rethrown exception: "; e
End Try

The inner Catch logs the error before rethrowing. The outer Catch then handles the same exception, showing how to implement layered error handling.

Best Practices

This tutorial covered FreeBasic exception handling with practical examples of Try/Catch/Throw 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.