Skip to content
Kavachकवच

Reading Errors

Also known as: error message compile error panic crash stack trace debugging fix typo did you mean

● Beginner5 minBefore this: Your First ProgramSpec 13

Error messages are help: each one says what is wrong, where, and usually what to write instead.

Everyone writes mistakes, every day. Kavach finds most of them before the program runs:

fn main() {
    const total = 40
    print(totl + 2)
}

The compiler refused the program:

kavach run typo.kav

error: `totl` is not defined; did you mean `total`?
typo.kav: Kavach code, then what running it prints

In the terminal, the message goes on: a line --> typo.kav:3:11 names the file, the line and the column, and the line itself is shown with ^^^^ under the mistake. Read the first line first: it is usually the fix.

Types that don't fit

fn main() {
    const age = 31
    const next: String = age + 1
    print(next)
}

The compiler refused the program:

kavach run types.kav

error: expected `String`, found `Int`
types.kav: Kavach code, then what running it prints

age + 1 is a number, but next was declared as text. Decide which you meant: a number, or String(age + 1).

Panics: stops while running

A few mistakes can only show while the program runs, such as a loop that counts one past the end of a list when the count comes from somewhere else. The program stops at once, with the reason, instead of carrying on with a wrong value:

fn show(names: List<String>, count: Int) {
    for i in 0..count {
        print(names[i])
    }
}

fn main() {
    show(["Asha", "Ravi"], 3)
}

The program ran, then stopped with a panic:

kavach run panic.kav

AshaRavipanic: index 2 is out of range: the list has 2 items
panic.kav: Kavach code, then what running it prints

When the compiler knows both the list and the range, the same loop is a compile error instead: see Kinds of Errors.

When a message isn't clear, that is a bug in Kavach: tell us.

Try it yourself

Fix the first program above so it prints 42.
Show the answer

Spell the name as it was declared: print(total + 2).

What Kavach does for you

  • Every index is checked

    Reading item 5 of a two-item list stops the program with the index and the list's length. It never reads memory that isn't the list's.

    spec 5.6

  • Typos get a suggestion

    Using an unknown name is an error that suggests the name you probably meant: did you mean total?

    spec 1.4