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

editing the error function

Started by Exerro, 06 January 2013 - 05:10 AM
Exerro #1
Posted 06 January 2013 - 06:10 AM
i am making a thingy like love2d but for computercraft but i want to have custom error catching…where you can disable the code stopping if a certain variable is true i.e. sky.finalRelease = true but im not sure how to 1) make a function that can terminate all running programs and quit and then display the error and 2) find the error function to copy&edit
Can someone either tell me where the error() code is or a way to stop all running programs and display what went wrong (just like the error() function)
Kingdaro #2
Posted 06 January 2013 - 08:22 AM
http://www.computercraft.info/forums2/index.php?/topic/7874-question-how-to-detect-if-a-program-crash/page__view__findpost__p__66717
Exerro #3
Posted 06 January 2013 - 09:13 AM
that would mean you had to do it with every function though? im making something like love2d so its basically just a bigger library with a couple of multitasking features and organisation features build in so making the user have to type that in every time would be pointless
Thanks though im sure it will help me with some other things
Does it check for errors through the whole code and even check functions in functions? i.e.

function f1()
    f2()
end
function f2()
    error here
end
ok, error = pcall(f1)
if not ok then
    print(error)
end
Kingdaro #4
Posted 06 January 2013 - 09:17 AM
I'm actually making a library similar to yours, except it's more of a literal remaking of love, haha.

But here's how I do things - I just wrap the operation of the user-defined functions in a pcall, instead of pcalling every single function.


local function run(fps)
  -- stuff that runs all of the functions
end

function start(fps)
  local ok, err = pcall(run, fps)
  -- have a function pcalling the running functions
end
Exerro #5
Posted 06 January 2013 - 09:20 AM
a) omg we should work together…my library addons are epic and it sound like ur organisation and multitasking(love.update,love.draw stuff ) etc are epic too
B)/><— stupid smileys(this is a b )) does pcall run the function or does it just check for errors
c) in java there is try and catch…is it possible to make a lua clone of this?

randomly spreading my code to everyone :P/> :
Spoiler

--This is BlueSky function library and event manager.
--Thank you for downloading this program
--Please give credit to awsumben13 if you use or distribute this program
--Help is at the bottom
local args = {...}
sky = {}
sky.clockload = nil
sky.graphicsload = nil
sky.fileload = nil
sky.exitx = 1
sky.exity = 1
function sky.update() end
function sky.input() end
function sky.draw() end
function sky.load() end
function sky.loadapibank() end
function sky.acquire(filename)
if filename and fs.exists(filename) then
  shell.run(filename,"LoadApiBank")
  return true
end
return false
end
function try(name,...)
ok, err = pcall(name,...)
if not ok then
  error(err)
end
end
--finish this!
function sky.error(input)
error(input)
end
function sky.inputter()
event,p1,p2,p3,p4,p5 = os.pullEvent()
if event ~= "timer" then
  sleep((1/sky.clock.fps)-timedif)
  sky.input(event,p1,p2,p3,p4,p5)
end
end
function sky.drawer()
sky.draw()
if sky.gameload then
  sky.game.draw()
end
end
function sky.updater()
time = os.clock()
try(sky.clock.updateTimers)
try(sky.clock.checkTimerOutput)
timedif = os.clock()-time
os.startTimer(1/sky.clock.fps-timedif)
try(sky.inputter)
try(sky.update,sky.clock.fps,)
try(sky.drawer)
term.setCursorPos(sky.exitx,sky.exity)
term.setBackgroundColour(colours.black)
term.setTextColour(colours.white)
end
opentype = args[1]
file = args[2]
if not file or file == "" or file == nil or #args < 1 then
k = sky.acquire "BlueSky/program.lua"
elseif not fs.exists(file) then
sky.error("file not found")
elseif fs.exists(file) then
k = sky.acquire(file)
end
if k and opentype == "run" then
table.remove(args,1)
table.remove(args,2)
sky.loadapibank()
if sky.clockload then
  sky.acquire "BlueSky/clock.lua"
end
if sky.graphicsload or sky.gameload then
  sky.acquire "BlueSky/graphics.lua"
end
if sky.fileload then
  sky.acquire "BlueSky/file.lua"
end
if sky.gameload then
  sky.acquire "BlueSky/game.lua"
end
if not sky.clockload then
  sky.clock = {}
  sky.clock.fps = 20
end
sky.acquire "BlueSky/event.lua"
sky.acquire "BlueSky/utils.lua"
sky.running = true
sky.load()
while sky.running == true do
  try(sky.updater)
end
end
Kingdaro #6
Posted 06 January 2013 - 09:27 AM
pcall runs the function as well as catching the errors. And honestly, pcall is basically the same thing as try/catch, so there wouldn't be much of a point. I'm interested if anyone else can implement sort of a makeshift try/catch, though.
Exerro #7
Posted 06 January 2013 - 09:28 AM
read a) and B)/>
edit: just a)
edit 2: so if there is an error…instead of error()ing it the pcall will stop the function and return what the error was?
Kingdaro #8
Posted 06 January 2013 - 09:35 AM
edit 2: so if there is an error…instead of error()ing it the pcall will stop the function and return what the error was?
Yep.

But eh, I usually work alone on things like this.
Exerro #9
Posted 06 January 2013 - 09:38 AM
:l k…is there any way to not let the program stop if there is an error? i.e. a critical error so term.setCursorPos(1) would just do nothing instead of error()ing or pcall()ing