# Try-Statements

## Try-statement syntax

```fsharp
try <expr> with <expr if error != nil>
```

**--OR--**

```fsharp
let <successVar, failureVar> = try <expr> with <expr>
```

The try-statement will evaluate the expression in-between `try` and `with`. Said expression must have a type of `(..., error)`.

* If `error != nil`: the expression to the right of `with` will be returned.
* If `error == nil`:
  * If you're using the first syntax: Control flow will continue as normal
  * If you're using the second syntax: expression between `try` and `with` will be destructured into `successVar` and `failureVar`&#x20;

## Working Example

{% tabs %}
{% tab title="Gauntlet" %}

```fsharp
package main

import "fmt" as fmt
import "errors" as errors

fun divide(num1: Int, num2: Int): (Int, Error) {
  if num2 == 0 {
    return (0, errors.new("Cannot divide by 0"))
  }
  return (num1 / num2, null)
}

fun divideBy2(num: Int): (Int, Error) {
  return divide(num, 2)
}

fun divideBy0(num: Int): (Int, Error) {
  return divide(num, 0) 
}

fun getResults(num: Int): (Int, Error) {
  let dBy2Res, dBy2Err = try divideBy2(5) with (dBy2Res, dBy2Err)
  let dBy0Res, dBy0Err = try divideBy0(10) with (dBy0Res, dBy0Err)
  return (dBy2Res + dBy0Res, null)
}

fun main(): Unit {
  fmt.println("Let's see if there's an error:")
  let res, err = getResults(10)
  fmt.println(err != null)
}
```

{% endtab %}

{% tab title="Transpiled" %}

```go
package main

import fmt "fmt"
import errors "errors"

func divide(num1 int, num2 int) (int, error) {
	if num2 == 0 {

		return 0, errors.New("Cannot divide by 0")
	}

	return num1 / num2, nil

}

func divideBy2(num int) (int, error) {

	return divide(num, 2)

}

func divideBy0(num int) (int, error) {

	return divide(num, 0)

}

func getResults(num int) (int, error) {
	dBy2Res, dBy2Err := divideBy2(5)
	if dBy2Err != nil {
		return dBy2Res, dBy2Err
	}
	dBy0Res, dBy0Err := divideBy0(10)
	if dBy0Err != nil {
		return dBy0Res, dBy0Err
	}

	// Eliminates any 'unused variable' errors
	_, _, _, _ = dBy0Err, dBy0Res, dBy2Err, dBy2Res
	return dBy2Res + dBy0Res, nil

}

func main() {
	fmt.Println("Let's see if there's an error:")
	res, err := getResults(10)
	fmt.Println(err != nil)
	// Eliminates any 'unused variable' errors
	_, _ = err, res

}

```

{% endtab %}

{% tab title="Output" %}

```
Let's see if there's an error:
true
```

{% endtab %}
{% endtabs %}


---

# 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://gauntletlang.gitbook.io/docs/advanced-features/try-statements.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.
