BlitzMax

BlitzMax

  • Downloads
  • Docs
  • API
  • Resources
  • About

›Language

Setup

  • Getting Started
  • Win32
  • Linux
  • macOS
  • Android
  • iOS
  • Raspberry Pi
  • NX (Switch Homebrew)
  • Custom Settings

Tutorials

  • Beginners Guide
  • OOP Tutorial
  • Network Programming
  • TCP Socket Programming

Language

  • Arrays
  • Basic Compatibility
  • Collections
  • Comments
  • Conditional Compiling
  • Constants
  • Data Types
  • Debugging
  • Enums
  • Exceptions
  • Expressions
  • Functions
  • Identifiers
  • Literals
  • Modules
  • Objects
  • Program Flow
  • Slices
  • Strings
  • User Defined Types
  • Variables
  • Advanced Topics

    • Interfacing With C
    • Memory Management
    • Pointers
    • Custom Pre/Post Compilation Scripts
    • Creating DLLs

Tools

  • MaxIDE
  • BlitzMax Make (bmk)
  • BlitzMax Compiler (bcc)
Edit

Exceptions

BlitzMax provides a mechanism for handling runtime errors known as exception handling.

An exception is actually just an object. You can throw and catch exceptions using the Throw command and a Try / Catch block. Here is a simple example:

Function ExTest()
    Throw "Bingo!"
End Function

Try
    ExTest()
Catch ex:Object
    Print ex.ToString()
End Try

Throwing an exception causes the program to jump to the most recent Catch block. You can provide multiple catch blocks for catching exceptions of different types. If there is no appropriate Catch block for the exception, then the program jumps to the next most recent Catch block. If no catch block can be found to deal with the exception, the program will end.

Finally

Finally is an optional block which can be used with Try / Catch and Try blocks. The Finally block is always the last block :

Try

Catch something

Finally

End Try

The Finally block always executes when the Try block exists. This is regardless of whether the Try block handles the exception or not. This feature can be useful as a way to always ensure some cleanup routine is always executed, even when no exceptions are anticipated.

The following is an example of a Try / Finally block used to guarantee the freeing of a lock before the method returns :

    Method process()
        Try
            Lock()

            ' do some stuff

        Finally
            Unlock()
        End Try
    End Method

In this example, even if an exception is raised during the processing, Unlock() will always be called before leaving the method.

Built-in Exceptions

BlitzMax has several built-in exception types, all of which extend type TBlitzException. These are:

ExceptionCause
TNullMethodExceptionThrown when a program attempts to call an abstract method.
TNullFunctionExceptionThrown when a program attempts to call a null function.
TNullObjectExceptionThrown when a program attempts to access a null object (only thrown in debug mode).
TArrayBoundsExceptionThrown when a program attempts to access an array element outside of an array's bounds (only thrown in debug mode).
TRuntimeExceptionThrown by the RuntimeError or Assert commands.
← EnumsExpressions →
  • Finally
  • Built-in Exceptions
BlitzMax
Docs
Getting StartedDownloadsAbout
Community
ResourcesSyntaxBomb Forums
More
GitHubStarChat on Discord
Copyright © 2023 Bruce A Henderson