Chapter 6. Handling and Avoiding Errors

Program correctness matters on every count, from user acceptance and trust to the downstream consequences of program output. In this chapter, you learn about the following:

  • The kinds of errors that can occur in your programs

  • Lua's mechanisms for handling errors

  • Techniques to avoid program errors

  • Techniques to locate program errors

Along the way, you'll become familiar with Lua's I/O library, which is included in some of the examples.

Kinds of Errors

Some flaws can turn up before a program is executed. These are compile-time errors, which Lua can find as it converts your source code into executable bytecode. Often these result from simple misspellings or typing slips as you edit your source code. Other flaws are more insidious—Lua recognizes them as syntactically correct so they make their way into the executable bytecode. These errors await discovery at run time, preferably by you during testing rather than by someone who's actually using a product after it's been released with the erroneous code.

Syntax Errors

Syntax refers to the way various language elements are allowed to fit together, completely independent from the meaning of those elements. A programming language like Lua is necessarily fussy about syntax, because source code expressed in it needs to translate unambiguously to bytecode, the instructions that Lua's virtual machine can deal with. During the 1960s (the renaissance era of modern programming languages) computer science practitioners ...

Get Beginning Lua Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.