Skip to the content.

Errors and exceptions

throw

Raise an exception with throw:

let f() = do
    throw MyError("something went wrong")

f()

try / catch

let f(x) = do
    x["hello"] = 1

try do
    f(10)
catch (Exception as e) do
    term.log(type(e).__name__ + ":", e.message)
    term.log("trace:", e.stack_trace)

(from exception_catch.ar)

Every caught exception object exposes at least:

A bare catch do ... (without (Exception as e)) also works when you don’t need the exception value:

let is_relative_to(p, base) = do
    try do
        relative_to(p, base)
        return true
    catch do
        return false

(from stdlib/path/init.ar)

Defining custom exceptions

Custom exceptions are just classes that inherit (directly or indirectly) from the built-in Exception type:

class CustomError(Exception) do

class MyError(CustomError) do

let f() = do
    throw MyError(`hel\u1324lo`)

f()

(from error_system.ar) — note that exception classes can have empty bodies; they inherit __init__, .message, .stack_trace, etc. from Exception.

Built-in exception types

The global scope defines a hierarchy of built-in exceptions you can catch or subclass:

Module-specific standard library packages (like file and regex) also define their own exception subclasses in an exceptions.ar file — see, for example, stdlib/file/exceptions.ar and stdlib/regex/exceptions.ar.

Next