# Error Handling

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

{% code overflow="wrap" %}

```rust
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
*/
```

{% endcode %}

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:

{% code lineNumbers="true" %}

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

    return a + b
}

add(5, 20).println()
```

{% endcode %}

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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dibs.gitbook.io/vortex-docs/language-reference/error-handling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
