All news

Published

Automatic error propagation with try

Jik v0.1.0-alpha.17 is now available. This release adds try error propagation, so a throwing function can pass a failed call to its caller without a local try / except block.

Propagate failures at the call site

In a throws func, write x := try f(...). On success, x is available in the enclosing scope; on failure, the current function returns through its error path immediately.

throws func div_safe(x, y):
    if y == 0:
        fail("division by 0")
    end
    return x / y
end

throws func half_of_safe_value():
    x := try div_safe(10, 2)
    return x / 2
end

Use try ... except ... end when a call should be handled locally; use declaration-form try when the enclosing throwing function should propagate the failure instead. This is useful because otherwise we would rapidly accumulate boilerplate of the following form:


throws func half_of_safe_value():
    try x: = div_safe(10, 2):
        return x / 2
    except:
        fail(error_msg())
    end
end