Error-handling in Lua
Lua does not officially support exceptions. But, a similar mechanism can be built with pcalls, or protected calls. An exception halts the flow of code in case of an error, and returns control to the caller right away. Furthermore, some kind of error code is likely provided to the caller. This method of raising errors immediately should, in theory, allow callers to handle unsafe code gracefully.
Note
Much like debugger.lua
, third-party Lua modules for profiling already exist. Unsurprisingly, one of the best modules for profiling is profile.lua
. You can learn more about profile.lua
and download it here: https://bitbucket.org/itraykov/profile.lua
pcall and error
Lua's most efficient way to handle errors is pcall
, or protected call. The pcall
function takes only one argument, the function to be called. Optionally, if the function takes arguments, they should also be passed to pcall
as additional arguments. On success, it returns true and all of the values the function would...