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 corresponding function from the library (lua_dofile, lua_dostring, lua_call, or lua_callfunction) is terminated returning an error condition.

The only argument to the ``error'' fallback function is a string describing the error. The standard I/O library redefines this fallback, using the debug facilities (see Section 7), in order to print some extra information, like the call stack. 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 routine is able to print also the lines where the calls (and the error) were made. If needed, it is possible to change the ``error'' fallback handler (see Section 4.7).

Lua code can explicitly generate an error by calling the built-in function error (see Section 6.1).


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