Contents

Error Handling

Koji has no exceptions and no try / catch. There are two kinds of failure:

KindExamplesHandling
Expected (recoverable)a missing file, invalid JSON, a network failure, int('abc')a Result value
Bug (unrecoverable)index out of range, division by zero, overflow, panic(...)panic: the program stops, it cannot be caught

Result

A function that can fail returns a Result[T, E]: Ok(value) on success, Err(error) on failure. The standard library uses typed errors, such as FileError or ParseError, so match can handle each case:

match (int('42')) {
    Ok(value) => print(value + 1)
    Err(InvalidNumber(text)) => print(`not a number: ${text}`)
    Err(OutOfRange(text)) => print(`too large: ${text}`)
}

Standard library errors have a readable message: printing or interpolating them shows it, and error.display() returns it (error.debug() gives the structure, InvalidNumber('abc')). A Result itself keeps its structural form:

match (int('abc')) {
    Ok(value) => print(value)
    Err(error) => print(error) // 'abc' is not a valid number
}

print(int('abc')) // Err(InvalidNumber('abc'))

When there is no value to give on success, the type is Result[void, E], and success is written Ok():

import file

function save(path: string, text: string): Result[void, file.FileError] {
    immut out = file.open(path, file.Write)?
    out.write(text)?
    return Ok()
}

The ? Operator

? propagates an error instead of handling it with match:

  • on a Result, value? gives the Ok value, or returns the Err from the function;
  • on an Option, value? gives the Some value, or returns None from the function.
function parseAge(text: string): Result[int, ParseError] {
    immut age = int(text)?

    return Ok(age)
}

print(parseAge('42')) // Ok(42)
print(parseAge('x')) // Err(InvalidNumber('x'))

The function must declare its return type, and it must be a Result (for ? on a Result) or an Option (for ? on an Option).

At the top level of the main file, an Err or a None stops the program, prints the error and exits with code 1:

immut n = int('oops')? // error: main.kj:1: 'oops' is not a valid number

Combining Errors of Different Types

A function can only return one error type. To combine errors from different sources, declare a sum type with one variant for each of them:

import file
import json

type ConfigError = {
    File(error: file.FileError)
    Json(error: json.JsonError)
}

function readConfig(path: string): Result[json.Json, ConfigError] {
    immut f = file.open(path, file.Read)? // a FileError becomes File(...)
    immut text = f.read()? // a FileError becomes File(...)

    f.close()

    return Ok(json.parse(text)?) // a JsonError becomes Json(...)
}

? wraps the error automatically when the function's error type has exactly one variant whose only field has the type of the error. Otherwise the conversion is ambiguous or impossible, and it is a compile error:

type E = {
    A(error: ParseError)
    B(error: ParseError)
}

function f(): Result[int, E] {
    return Ok(int('1')?) // Error! the variants A, B all hold a ParseError
}

mapErr

result.mapErr(f) converts the error of a Result with a function (or a variant with one field), and keeps an Ok as it is:

function parse(text: string): Result[int, string] {
    immut n = int(text).mapErr((error: ParseError) => `bad number: ${text}`)?

    return Ok(n)
}

print(parse('x')) // Err('bad number: x')

Panics

panic(message) stops the program. Use it for states that should be impossible, not for expected failures:

function check(age: int): int {
    if (age >= 0) { return age }

    panic('age cannot be negative')
}

Edit this page on GitHub