pcall

Calls func (with arguments as parameters) in protected modeThat is, any errors thrown while the function is executing are caught and returned by the nearest enclosing pcall.

ExampleCatch errors from a named function
Execution of badFunction will throw an error (and abort the program), but running it within pcall allows the programmer to make a decision about the error
Code
<nowiki>
local function codeSnip()
  error("Some error message")
end

local ok, err = pcall(codeSnip)
if not ok then
  print("Execution of badFunction errored with " .. err)
else
  print("Function returned normally the value " .. err)
end
    </nowiki>
Output
<nowiki>
Execution of badFunction errored with Some error message
</nowiki>

pcall
Function
Syntax
pcall(
  • func : function
  • arguments... : any
)

Returns bool false, any error | bool true, any returns
API Base globals
Source Lua (source)
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.