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.
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.
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.
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 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.
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.
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.
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
- Specific exceptions: Catch the most specific exception type first.
- Meaningful messages: Provide descriptive exception messages.
- Resource cleanup: Use Finally for releasing resources.
- Don't ignore: Avoid empty Catch blocks that swallow exceptions.
- Documentation: Document which exceptions functions can throw.
This tutorial covered FreeBasic exception handling with practical examples of Try/Catch/Throw usage in different scenarios.
Author
List all FreeBasic Tutorials.