Error Handling

Managing runtime errors gracefully.

Panics

When Ry encounters an unrecoverable situation (like dividing by zero or calling a variable that isn't a function), it triggers a panic. A panic typically stops the program execution and prints an error message with a stack trace.

You can also manually trigger a panic in your code using the built-in panic keyword.

func divide(data a, data b) {
    if (b == 0) {
        panic "Cannot divide by zero!"
    }
    return a / b
}

Attempt and Fail

To handle errors gracefully without crashing the entire program, Ry provides the attempt and fail blocks.

Code inside the attempt block is executed safely. If a panic occurs within that block, execution immediately jumps to the fail block.

Capturing the Error

The fail block can optionally accept a variable name, which will be assigned the error message associated with the panic.

attempt {
    # This will cause a panic
    out(10/0)
} fail err {
    out("Caught error: ${err}")
}

out("Program continues...")

Now that you can handle errors, let's look at how to organize your code with Namespaces.

Next: Namespaces!