Posted 15 November 2012 - 11:54 AM
Is there any way for the computer to read if an error happens and turn it into an action, such as turning a light from green to red?
local function codeBlock()
local a = "1"+1 -- some error
error("A way to raise an error.")
return 42
end
local ok, result = pcall( codeBlock )
if not ok then -- error has occured
print( result ) -- print the error
-- do more
else
-- no error, result is now the return value from the function codeBlock
end
You can catch errors from a function and then do somethig with it. An example:local function codeBlock() local a = "1"+1 -- some error error("A way to raise an error.") return 42 end local ok, result = pcall( codeBlock ) if not ok then -- error has occured print( result ) -- print the error -- do more else -- no error, result is now the return value from the function codeBlock end