Internally, Lua uses the C longjmp
facility to handle errors. (You can also choose to use exceptions if you use C++; see file luaconf.h
.) When Lua faces any error (such as memory allocation errors, type errors, syntax errors, and runtime errors) it raises an error; that is, it does a long jump. A protected environment uses setjmp
to set a recover point; any error jumps to the most recent active recover point.
Almost any function in the API may raise an error, for instance due to a memory allocation error. The following functions run in protected mode (that is, they create a protected environment to run), so they never raise an error: lua_newstate
, lua_close
, lua_load
, lua_pcall
, and lua_cpcall
.
Inside a C function you can raise an error by calling lua_error
.