Errors and optional results

This chapter introduces use cases related to errors and optional values, including special uses of switch and try constructs.

There are two primary types of errors:

  • Program-generated errors that are returned as values.

  • Errors that result from exceptional control flow operation.

  • switch supports general pattern-matching; point to broader discussion of switch and pattern matching (next chapter)

  • handle errors as values with switch (not with try)

  • design question in an API: When to use which?

  • handle errors as exceptions with try --- in a way, these are less general for API design; may only appear in certain (async) contexts. But they can recover from errors that would otherwise be fatal (such as system errors), so they are necessary.

  • discuss programming examples:

  • discuss use of Result module, with examples

  • discuss use of Option module, with examples

  • discuss use of Error type; exceptions versus traps (is there a difference?)