This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CitricThunder's profile picture

[QUESTION] Make something happen when an error occurs?

Started by CitricThunder, 15 November 2012 - 10:54 AM
CitricThunder #1
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?
Orwell #2
Posted 15 November 2012 - 12:07 PM
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
CitricThunder #3
Posted 15 November 2012 - 12:48 PM
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

Thanks!