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 normally return. On failure, it returns false, and any error messages that need to be returned as well.
When an error happens, the error function can be used to stop the execution of the current method. The error function expects only one argument (and even that's optional): an error message. This message can be a string, but doesn't have to be. The error message can be any valid value. The code here shows a simple ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access