Error Handling

Whenever an error occurs during Lua compilation or execution, the function variable _ERRORMESSAGE is called (provided it is different from nil), and then the corresponding function from the library (lua_dofile, lua_dostring, lua_dobuffer, or lua_call) is terminated, returning an error condition.

Memory allocation errors are an exception to the previous rule.When memory allocation fails, Lua may not be able to execute the _ERRORMESSAGE function. So, for memory allocation errors, the corresponding function from the library returns immediately with a special error code (LUA_ERRMEM).

_ERRORMESSAGE (ERRORSTRING)

A variable holding the error handler function for non-memory errors.Can be set by the user to provide customized error handling.Accepts a string argument describing the error. By default, this function variable is set to the basic library function _ALERT, which prints the message to stderr. The standard I/O library redefines this function and uses the debug facilities to print some extra information, such as a call stack traceback.

Explicit errors can be generated by calling error. Lua code can “catch” an error using the function call . See the basic function library for a description of these functions.