Programming in Lua - 8.3
Programming in Lua - 8.3
Programming in Lua
Part I. The Language
Chapter 8. Compilation, Execution, and
Errors
8.3 – Errors
Errare humanum est.
Therefore, we must handle errors the best way we can.
Because Lua is an extension language,
frequently embedded in an application,
it cannot simply crash or exit when an error happens.
Instead, whenever an
error occurs,
Lua ends the current chunk
and returns to the application.
n = io.read("*number")
The assert function checks whether its first argument is not false
and simply
returns that argument;
if the argument is false (that is, false or nil),
assert
raises an error.
Its second argument, the message, is optional,
so that if you do
not want to say anything in the error message,
you do not have to.
Beware,
however, that assert is a regular function.
As such, Lua always evaluates its
arguments before calling the function.
Therefore, if you have something like
n = io.read()
assert(tonumber(n),
...
...
repeat
until file