Skip to content

Error Handling

Handle errors gracefully with try-catch blocks.

Basic Try-Catch

try {
    print "Attempting operation...";
    throw "Something went wrong!";
    print "This won't execute";
} catch err {
    print "Error caught: #{err}";
}

print "Program continues";

Practical Example

fn divide_safe(a, b) {
    try {
        if b == 0 {
            throw "Division by zero";
        }
        return a / b;
    } catch err {
        print "Error: #{err}";
        return 0;
    }
}

print divide_safe(10, 2);  // 5
print divide_safe(10, 0);  // 0 (with error message)