---
title: Errors
description: Handle Tide errors as values with fails, fail and try, and use
  optional values to represent data that may be missing.
source_url: https://tide-engine.dev/language/errors
source_file: docs/language/errors.md
source_revision: d46b7bc81b49e31e268bd62d158c040ec1c9ff21
source_worktree: modified
documentation_sha256: cff5cc1f43e9cab7c438614cde0e25d6b097b0171d16e0bfbecd7198624f4f9d
documentation_scope: Website source checkout; not matched to an installed Tide
  release. Check tide version and the project toolchain.
---

<a id="errors"></a>

# Errors

Errors in Tide are values, never exceptions. A function that can fail says so, and the code that calls it decides what to do about it, right where it calls.

<a id="functions-that-fail"></a>

## Functions that fail

`fails` after a function's parameters says it can fail, and with what. `fail` ends it with an error, as `return` does with a value:

```csharp
enum ParseError
{
    Empty,
    NotANumber,
}

int ParseScore(string text) fails ParseError
{
    if (text == "") fail ParseError.Empty;
    mut var score = 0;
    for (var i = 0; i < text.length; i++)
    {
        var digit = "0123456789".IndexOf(text.Substring(i, 1));
        if (digit < 0) fail ParseError.NotANumber;
        score = score * 10 + digit;
    }
    return score;
}
```

An error is any type. Usually it's an enum, which names each way the function can fail. When it needs to say more, it's a struct:

```csharp
struct Refused
{
    int code;
    string message;
}

void Connect(int port) fails Refused
{
    if (port == 0) fail Refused { code = 1, message = "no port" };
}
```

A function that returns nothing fails the same way, and succeeds when it reaches its end. Methods can fail too.

<a id="handling-an-error"></a>

## Handling an error

What a failing call gives is its value or its error. Before its value can be used, the call is unwrapped, in one of four ways:

```csharp
// ?? falls back to another value when it fails.
var fallback = ParseScore(text) ?? 0;

// is runs code only when it succeeds, with its value...
if (ParseScore(text) is int score)
{
    total += score;
}

// ...or only when it fails, with its error.
if (ParseScore(text) is ParseError why) { ... }
if (ParseScore(text) is ParseError.Empty) { ... }

// ! carries on with the type's default (0 for an int) when it fails.
var parsed = ParseScore(text)!;

// try passes the error on to the caller, which fails with the same type.
int Doubled(string text) fails ParseError
{
    var score = try ParseScore(text);
    return score * 2;
}
```

- `??`'s right side only runs when the left fails, so, like the right side of `&&`, it can't spawn or draw widgets. Chains work: `ParseScore(a) ?? ParseScore(b) ?? 0`.
- A name after `is` goes in an `if`'s or a loop's condition, alone or joined with `&&`. It's in scope where the test is true: `if (ParseScore(t) is int score && score > 10)`, or `while (Next() is int item) { ... }`. Without a name, `is` is a test anywhere: `var empty = ParseScore(t) is ParseError.Empty;`.
- `!` never crashes. A struct's default is its fields' defaults, as `Stats { }` makes them.
- `try` binds like a unary operator, so `try ParseScore(a) + 1` adds 1 to the value. It only works in a function or method that fails with the same error type. Systems, views and handlers have no caller to pass an error to, so they handle it where they call.

`var` holds what a call gives as it is, to unwrap later:

```csharp
var result = ParseScore(text);
if (result is ParseError why) { ... }
var score = result ?? 0;
```

Using a failing call's value without unwrapping it is an error that says how to unwrap it. Calling one as a statement and ignoring its error is a warning; `!` says you meant to:

```csharp
Connect(7777);   // Warning: nothing handles its error
Connect(7777)!;  // Carries on, whatever happened
```

<a id="values-that-may-be-missing"></a>

## Values that may be missing

`T?` is a value or nothing, for lookups where nothing isn't an error. `null` is nothing, and a value converts to a `T?` by itself:

```csharp
int? Find(List<int> items, int wanted)
{
    for (var i = 0; i < items.count; i++)
    {
        if (items[i] == wanted) return i;
    }
    return null;
}

var index = Find(items, 3) ?? -1;
if (Find(items, 3) is int at) { ... }
if (Find(items, 3) == null) { ... }
```

It unwraps the same way, with `??`, `is` and `!`, but not `try`: there's no error to pass on. Functions can return a `T?` and take one, and locals can hold one: `int? best = null;`.

<a id="fields-that-may-be-missing"></a>

### Fields that may be missing

A field can be a `T?` too, in a struct, a component, a singleton, a scene, an event or the input, and so can a value a [command](https://tide-engine.dev/game/commands.md) is called with:

```csharp
struct Slot
{
    int? item;          // Nothing, until something sets it
    string? label;
    int? level = 3;     // A default, as on any field
}

component Unit
{
    Entity? target;
    Slot slot;
}

system Aim(mut Unit unit)
{
    unit.target = null;                       // Nothing
    unit.slot.item = 4;                       // A value
    unit.slot = Slot { label = "sword" };     // Fields left out have nothing, or their default
    var item = unit.slot.item ?? 0;           // Read as any T?
    if (unit.slot.label is string label) Debug.Log(label);
    if (unit.target == null) { ... }
}
```

- A field with no default starts with nothing, and so does one a struct's value leaves out.
- It's set to a value, another `T?` or `null`, and read with `??`, `is`, `!` and `== null`, like a local. A `var` takes it as the `T?` it is.
- It works wherever its type would without the `?`. An [input](https://tide-engine.dev/game/input.md) holds no text, so it holds no `string?` either.
- It can't be a grid or a rendering resource, which are never missing: a grid is empty, and a `Texture` is `default`.
- It can't be passed as a `mut` or `out` argument. Pass a local, and assign the field after.
- A list, a map or an array doesn't hold a `T?`. It holds a struct with one: `List<Slot>`, and `slots[i].item = 3`.
- In text, a struct shows a field with nothing as `null`: `Slot { item = null, label = "sword", level = 3 }`.
- In a view, a `T?` field isn't blended between ticks: it's the newer tick's, a value or nothing.
- Frags and verts can't read one, or take a struct that has one. A component or singleton with one is still fine there for its other fields.

In a match, a `T?` field with nothing costs a bit in a [command](https://tide-engine.dev/game/commands.md), and its storage is all zeros, so setting a field and then taking it away leaves no trace of what it held.

<a id="what-doesn-t-fail"></a>

## What doesn't fail

Most of Tide never fails, so there's nothing to handle:

- Arithmetic wraps on overflow, and dividing by zero gives 0.
- Reading a list past its end gives its element type's zero, and writing there does nothing.
- Text clamps positions past its end.
- `Math` is forgiving with bad values: `Math.Clamp` always returns a value in range.

Failures that happen later, rather than during a call, are events: a match that ends or a server that goes away sends `Disconnected` (see [Matches](https://tide-engine.dev/multiplayer/matches.md)).
