3.6 Error Handling in C

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

3.6 Error Handling in C

3.6 Error Handling in C

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.

Most functions in the API can throw an error, for instance due to a memory allocation error. The documentation for each function indicates whether it can throw errors. The next functions works in the protected mode and do not generate error situations: lua_newstate , lua_close , lua_load , lua_pcall , and lua_cpcall .

Inside a C function you can throw an error by calling lua_error .