/ Error Handling
Documentation

Getting Started

Language Guide

SDL2 and Graphics

CLI Reference

Error Handling

Vexel provides two mechanisms for dealing with errors at runtime: the assert statement for invariant checks, and try/catch for recoverable runtime errors.

assert

assert checks that a condition is true at runtime. If the condition is false, the program aborts with an error message. It is used to enforce preconditions and catch programming mistakes early.

# assert with no message
assert x > 0

# assert with a custom error message
assert health > 0, "health must be positive"

fn divide(a: int, b: int) -> int:
    assert b != 0, "cannot divide by zero"
    return a / b

Assertions are not removed in optimized builds. Use them to document and enforce invariants that should never be violated.

try / catch

The try/catch block handles runtime errors that can be recovered from. The catch block receives the error as a string variable.

try:
    let result: int = risky_operation()
    print(result)
catch err:
    print("Error: ")
    print(err)

The error variable (err above) is a str describing what went wrong. It is scoped to the catch block.

← Interfaces Imports →