githubEdit

Error Handling

Stof throw & try-catch

Errors in Stof use a common try-catch paradigm. Whenever a function cannot be found at runtime, an operator isn't supported, or any other issue occurs, Stof will throw an error that you can optionally catch.

#[main]
fn main() {
    try throw();
    catch {
        /* recover from an error */
    }
}

Throw

When using Std.throw(v: unknown) to throw an error, any Stof value can be passed into the function - this is the value that can be caught.

#[main]
fn main() {
    try {
        throw("CustomError");
    } catch (error: str) { // will try casting the error to a str if needed
        switch (error) {
            case "CustomError": {
                pln("handling the custom error");
            }
            default: {
                pln("optional handling of an unknown error");
            }
        }
    }
}

As you can imagine, this gets pretty powerful in practice, allowing you to pass objects, maps, functions, etc. back up the stack as an error.

Last updated