> For the complete documentation index, see [llms.txt](https://docs.ethscriptions.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ethscriptions.com/ethscriptions-vm/rubidity/error-handling.md).

# Error Handling

Rubidity provides a set of tools to help you write secure and reliable smart contracts. This includes native support for preconditions via `require` statements and automatic type checking for variables and parameters.

#### Require Statements

The `require` function is used to ensure that certain conditions are met before a function proceeds. If the condition specified in `require` is false, the function will throw an error, and all changes to the state will be reverted.

Here's an example that demonstrates the use of `require` in a `mint` function:

```ruby
function :mint, { amount: :uint256 }, :public do
  # Ensure the mint amount is positive
  require(amount > 0, 'Amount must be positive')
  
  # Ensure the mint amount does not exceed a pre-defined per mint limit
  require(amount <= s.perMintLimit, 'Exceeded mint limit')

  # Ensure the total supply won't exceed the max supply
  require(s.totalSupply + amount <= s.maxSupply, 'Exceeded max supply')
  
  # Proceed to mint
  _mint(to: msg.sender, amount: amount)
end
```

In this example, the `require` statements act as guards that prevent undesirable actions. If any of the `require` statements fail, the function will terminate, providing the string message as an error reason.

#### Type Checking

Rubidity also performs automatic type checking on function parameters and state variables. This means that if you declare a variable as a `:uint256`, Rubidity will ensure that you can only assign unsigned 256-bit integers to that variable. Any attempt to assign a different type will result in a runtime error.


---

# 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:

```
GET https://docs.ethscriptions.com/ethscriptions-vm/rubidity/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.
