Lyricdev
GitHub

LYR-SEM0035

a catch-all clause before another clause

error ยท spec 06-errors

A catch clause without a type, which takes every error, is followed by further catch clauses.

Explanation

Clauses are tried in order, and a clause without a type takes every error, so a clause after it could never run.

Put the catch-all last, after the clauses for particular types.

Wrong

class Disk :: [Error] { fn message(): string { return "disk"; } }
class Net :: [Error] { fn message(): string { return "net"; } }
fn read(n: int): int throws [Disk, Net] {
    if (n > 0) { throw Net { }; }
    throw Disk { };
}

fn main(): void {
    try { let _ = read(1); }
    catch (_) { }
    catch (_: Disk) { }
}
class Disk :: [Error] { fn message(): string { return "disk"; } }
class Net :: [Error] { fn message(): string { return "net"; } }
fn read(n: int): int throws [Disk, Net] {
    if (n > 0) { throw Net { }; }
    throw Disk { };
}

fn main(): void {
    try { let _ = read(1); }
    catch (_: Disk) { }
    catch (_) { }
}