10. Reading error messages

When a program is wrong, Lua does not stay silent — it prints an error message and stops. Beginners see that red text as scary noise and start changing things at random. But it is not noise: Lua is telling you exactly which line to look at and hinting at what is wrong. This chapter teaches you to read it.

The shape of an error

Almost every Lua error has the same shape:

lua: file.lua:LINE: message

Three parts, and you read them in this order:

  1. The line number (file.lua:3 means line 3). Look there first. Always.
  2. The message — a short description of what went wrong.
  3. Sometimes a stack traceback below it. As a beginner, ignore the traceback; the first line has what you need.

Four errors you will actually hit

These four cover most early mistakes. Learn to recognise them by sight.

Unfinished string

You forgot a closing quote.

print("hi)
lua: hello.lua:1: unfinished string near '"hi)'

"Unfinished string" means Lua found an opening quote and reached the end of the line without finding the closing one. Fix: add the missing ".

Attempt to call a nil value

You spelled a function name wrong.

prnt("hi")
lua: hello.lua:1: attempt to call a nil value (global 'prnt')

Lua is saying: you tried to call prnt, but there is no such thing, so it is nil, and you cannot call nil. The name in quotes ('prnt') is the misspelled word. Fix: spell print.

Attempt to concatenate a nil value

You glued .. onto something that has no value.

print("score: " .. nil)
lua: hello.lua:2: attempt to concatenate a nil value

Something on one side of .. is nil — often a variable you never gave a value, or whose name is misspelled. Fix: make sure both sides hold a string or number.

'end' expected

You opened an if, for, while, or function and never closed it.

if x > 3 then
  print("big")
lua: hello.lua:3: 'end' expected (to close 'if' at line 1) near <eof>

This one is helpful: it tells you which block was left open and on what line it started (to close 'if' at line 1). <eof> means "end of file" — Lua reached the bottom still waiting for an end. Fix: add the end.

The edit–run–fix loop

The right way to deal with errors is not to spot them all by eye, but to run a tight loop:

  1. Run the file.
  2. Read the first error: line number, then message.
  3. Fix that one thing.
  4. Run again.

Repeat until it runs clean. Do not fix three things at once — one "fix" might be wrong, and then you cannot tell which change helped. One error, one fix, run again.

A single mistake sometimes produces a confusing message, or one that points at the line after the real problem (a missing end is often reported at the end of the file). If the named line looks fine, check just above it.

Open exercises/10/01-three-bugs.lua. It does not run. Use the edit–run–fix loop: run it, read the first error, fix only that, run again. Keep going until it prints All fixed!.

Errors are not failure

Every programmer reads error messages every day. They are not a sign you are bad at this — they are the normal back-and-forth of writing code. Reading errors calmly just means you have read a few thousand of them. You are on your way.

Homework

Homework files are in exercises/10/homework/.

Problem 1 — Name the error

Open exercises/10/homework/01-name-the-error.lua. It contains one broken line. Run it, read the message, and (in a comment at the bottom) write which of the four error types from this chapter it is. Then fix it.

Problem 2 — Fix the quote

Open exercises/10/homework/02-fix-the-quote.lua. It has an unfinished string. Fix it so the program prints the quote on one line.

Problem 3 — Fix the typo

Open exercises/10/homework/03-fix-the-typo.lua. A function name is misspelled, giving an "attempt to call a nil value" error. Fix it.

Challenge — Three in a row

Open exercises/10/homework/04-three-in-a-row.lua. It has three separate mistakes, one of each: an unfinished string, a misspelled print, and a missing end. Fix them one at a time, running after each fix, until the file prints:

done

Stuck or finished? Open the homework solutions page.