personal-page/_master_wiki/void/notes/error-handling.md

1.4 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 called Ok(T)), or can hold an error.
  • Option<T>: this data can have a value (sometimes called Some(T)) or can not have a value (sometimes called None()).

This data type is often used with pattern matching, allowing to handle each possible variation of the type.

Resources:

Return touples