> For the complete documentation index, see [llms.txt](https://docs.budgielang.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.budgielang.org/syntax/exceptions.md).

# Exceptions

All languages have some way of representing a breaking error state, or exception, that indicates control flow must be halted. Exceptions may be created and thrown, also known as raised, and later caught, also known as rescued, by some calling code. Budgie refers to these operations as catching and throwing exceptions.

## Throwing

The built-in exception class for an output language is represented in Budgie by the `exception` command, which receives a single string as input. It can be thrown with the `throw` command.

```
throw : { exception } ("Oh no!")
```

In C#:

```csharp
throw new Exception("Oh no!");
```

In Python:

```python
raise Exception("Oh no!")
```

## Catching

All supported languages have some variant of the following three code blocks:

* Try: runs some code that might throw an error
* Catch: handles any error thrown by the try section
* Finally: runs regardless of whether an error was thrown

Each of these are considered their own distinct blocks with a `start` and `end` in Budgie. The `catch` section also takes in the name of a general exception.

```
try start
    throw : { exception } ("Oh no!")
try end
catch start : error
    print : ("Found an error.")
catch end
finally start
    comment line : ...
finally end
```

In C#:

```csharp
try
{
    throw new Exception("Oh no!");
}
catch (Exception error)
{
    Console.WriteLine("Found an error.");
}
finally
{
    // ...
}
```

In Python:

```python
try:
    raise Exception("Oh no!")
except Exception as error:
    print("Found an error.")
finally:
    # ...
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.budgielang.org/syntax/exceptions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
