Skip to main content
Baretta does not have try and except. Instead, operations that can fail return a Lookup. This includes find on a dict or list, and any function you write that can fail. A Lookup[uint256] is a lookup that holds a uint256 value when it succeeds.
The other ways to read a value (hello[key] and hello.get(...)) are described in Lists and dicts.

Methods

The reason of a missing key names the dict and the key, for example key 'base' not found in 'hello'.

Handling an error

Use default to set a fallback value:
For a dict or list, hello.get("base", 100) does the same and is shorter.
Or check with if:
Or stop with your own message. This is useful on-chain, where the message ends up in the revert reason:
If you call ok() directly and the lookup is an error, the program crashes:
On a node, a crash reverts the whole transaction. In the terminal, Baretta prints the reason and exits with an error code. The code you write is the same in both places.

Returning a Lookup

A function can return a Lookup. Use Ok(value) for success and Err(reason) for failure.
The error is always a text message, you cannot customize the error type. The caller uses the same methods as above:

A Lookup is not its value

You cannot use a Lookup where a value is expected. The compiler rejects it and tells you how to fix it:
In the future, we will add other handling objects to achieve efficiency.