Lyricdev
GitHub

LYR-SEM0171

a catch-all clause that nothing reaches

warning · spec 06-errors · lint unreachable-catch, group unused, warn unless said

A catch clause without a type (catch (_)) follows clauses that already take every error type the try block throws.

Explanation

The clauses of a try are tried in order, and a clause without a type takes what the clauses above it leave. When they take every type the block can throw, the last clause is dead code.

Remove the clause.

Wrong

struct NotFound :: [Error] {
    path: string,

    fn message(): string {
        return f"no {this.path}";
    }
}

fn find(p: string): int throws NotFound {
    if (p == "") {
        throw NotFound { path = "empty" };
    }
    return 1;
}

fn main(): void {
    var n = 0;
    try {
        n = find("");
    } catch (e: NotFound) {
        n = e.path.length();
    } catch (_) {
        n = -1;
    }
    assert(n == 5);
}
struct NotFound :: [Error] {
    path: string,

    fn message(): string {
        return f"no {this.path}";
    }
}

fn find(p: string): int throws NotFound {
    if (p == "") {
        throw NotFound { path = "empty" };
    }
    return 1;
}

fn main(): void {
    var n = 0;
    try {
        n = find("");
    } catch (e: NotFound) {
        n = e.path.length();
    }
    assert(n == 5);
}