Lyricdev
GitHub

LYR-SEM0034

an error that nothing handles

error · spec 06-errors

A call, throw or other site may throw an error type that no surrounding catch takes and the enclosing function's throws does not declare. In a lambda, an initializer or a detached body, where nothing can be declared, the error has to be caught there.

Explanation

Errors are checked: every error a site may throw is caught around it or declared in the throws of the function it stands in, and the caller then handles it in turn. A global or constant initializer, and main for anything but Cancelled, have no caller to pass it to.

Catch the error (try … catch, try?), or add its type to the function's throws.

Wrong

enum Oops :: [Error] { Bad; fn message(): string { return "oops"; } }

fn risky(): int throws Oops { return 1; }

fn twice(): int { return try risky() * 2; }

fn main(): void {
    let _ = twice();
}
enum Oops :: [Error] { Bad; fn message(): string { return "oops"; } }

fn risky(): int throws Oops { return 1; }

fn twice(): int throws Oops { return try risky() * 2; }

fn main(): void {
    let _ = try? twice();
}