Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Error Handling

This chapter defines Forge’s error handling mechanisms. Forge uses a multi-layered approach: Result types for explicit error values, the ? operator for propagation, safe blocks for error suppression, must for crash-on-error semantics, and check for declarative validation.

Overview

Forge provides five complementary error handling mechanisms:

MechanismPurposeBehavior on Error
Result typeRepresent success/failure as valuesCarries error as data
? operatorPropagate errors up the call stackReturns Err from enclosing function
safe { }Suppress errors silentlyReturns null
must exprAssert success or crashRaises a runtime error
check exprDeclarative validationRaises a runtime error with description

These mechanisms serve different use cases:

  • Result + ? — For functions that can fail and callers that want to handle failures explicitly.
  • safe — For optional operations where failure is acceptable and the value can be null.
  • must — For operations that should never fail in correct code.
  • check — For input validation with readable error messages.

Runtime Errors

All Forge runtime errors are represented by RuntimeError, which contains a message string and an optional propagated value. When an error is not caught, it terminates the program with an error message.

Errors can be caught with try/catch:

try {
    let x = 1 / 0
} catch e {
    println(e.message)    // "division by zero"
    println(e.type)       // "ArithmeticError"
    println(e.code)       // "E0008"
    println(e.hint)       // "check that the divisor is not zero before dividing"
}

The catch variable receives an object with four fields, in this order:

  • message — the error message without its hint line (usually one line; an assertion’s left:/right: lines are kept).
  • type — a category of the code (table below). Prefer code.
  • code — the stable error code (E0008), as forge explain lists it.
  • hint — the suggestion the error carries ("available fields: a, b", "did you mean 'count'?"), or null when it has none.

The type is a category of the code, so every error with one code has the same type:

Error TypeCodes
TypeErrorE0004 E0005 E0006 E0007 E0011 E0012 E0014 E0015 E0017 E0028 E0029 E0034
ReferenceErrorE0003 E0010 E0013 E0019
IndexErrorE0009
ArithmeticErrorE0008
ValueErrorE0038 (not a number), E0039 (invalid JSON), E0040 (invalid regex)
AssertionErrorE0021 E0022
TimeoutErrorE0031 E0036
ConnectionErrorE0037
RuntimeErrorevery other code (E0000, E0016, E0023, E0041, …)

Subsections

The following subsections define each error handling mechanism in detail: