Error Handling

Vortex allows you to safe-guard against errors using try/catch.

const add = (a, b) => {
    try {
        return a + b
    } catch (e) {
        println(e)
        return (-1)
    }
}

add(5, 10).println() // 15

add(5, "a").println()

/*
{ message: Cannot perform operation '+' on values: 5 (Number), a (String), type: GenericError, line: 52, path: src/main.vtx }
-1
*/

Hitting the catch block exposes the error object, and you can handle it in any way you choose.

To throw an error, simply return an Error() call:

const add = (a, b) => {
    if (b > 10) {
        return Error("'b' must be less than 10", "ValueError")
    }

    return a + b
}

add(5, 20).println()

Output:

ValueError: 'b' must be less than 10
[line 52] in src/main.vtx:3 <add>
[line 58] in src/main.vtx:9

Note that when an error is not handled, the program will exit and print the stack trace to the console. Here is an example of a deeply nested error:

GenericError: Global 'tweenf' is undefined
[line 222] in /Dev/app/components/gui/animation.vtx:222
[line 151] in /usr/local/share/vortex/modules/functional/functional.vtx:151 <forEach>
[line 221] in /Dev/app/components/gui/animation.vtx:221 <play>
[line 133] in /Dev/app/components/gui/animation.vtx:133 <run>
[line 112] in /Dev/app/components/Counter.vtx:112 <Counter>
[line 75] in /Dev/app/components/App.vtx:75 <App>
[line 311] in /Dev/app/components/gui/core.vtx:311 <Main>
[line 9] in src/main.vtx:9

Last updated