Next: 5 The Application Program Up: 4 The Language Previous: 4.7 Fallbacks

4.8 Error Handling

Because Lua is an extension language, all Lua actions start from C code calling a function from the Lua library. Whenever an error occurs during Lua compilation or execution, an error fallback function is called, and then the correspondent function from the library (lua_dofile, lua_dostring, lua_call, and lua_callfunction) is terminated returning an error condition.

The only argument to the error fallback function is a string describing the error and some extra informations, like current line (when the error is at compilation) or current function (when the error is at execution). For more information about an error, the Lua program can include the compilation pragma $debug. This pragma must be written in a line by itself. When an error occurs in a program compiled with this option, the error message includes extra information showing the stack of calls.

The standard error routine only prints the error message to stderr. If needed, it is possible to change the error fallback routine; see Section 4.7.

Lua code can generate an error by calling the function error. Its optional parameter is a string, which is used as the error message.

Next: 5 The Application Program Up: 4 The Language Previous: 4.7 Fallbacks