1.3 KiB
1.3 KiB
| title | tags | created | |||
|---|---|---|---|---|---|
| Error Handling | 2024-10-20 16:45 |
Error Handling
Different ways to handle errors in code
Errors as Exceptions - Try Catch
Try Catch
Errors as Values
Result<T, Err> - Option<T>
The Result<T, Err> type (sometimes also called Option<T>, Maybe<T>, or others)
is a custom data type that may or may not have a value, but like the
Schrödinger's box, we won't know until we peak inside of it, forcing the code
to handle both cases.
This data type can be use to solve 2 common pitfals:
Result<T, Err>: this data can have a value (sometimes calledOk(T)), or can hold an error.Option<T>: this data can have a value (sometimes calledSome(T)) or can not have a value (sometimes calledNone()).
This data type is often used with pattern matching, allowing to handle each possible variation of the type.
Resources:
- Readwise/Monad Is Actually Easy.
- Rust
Option<T>andResult<T, E>types. - Gleam
Option(a)type.