This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Error handling no function
Started by danielsv03, 17 June 2017 - 10:37 AMPosted 17 June 2017 - 12:37 PM
Hello so i used a error handling before on an function but the thing is the the main part of the script can't be in a function but i also want it to cath errors in a custom error screen right now its just the normal what is the solutions to this?
Posted 17 June 2017 - 02:12 PM
Put the main part of the script in a function. It can be in one.
Posted 17 June 2017 - 02:51 PM
You have something like this:
What KingofGamesYami is suggesting is something like this:
print("hello world")
What KingofGamesYami is suggesting is something like this:
local function main()
print("hello world")
end
local ok, err = pcall(main)
if not ok then
--# handle error
print("error: "..err) --# print out the error
end
Edited on 17 June 2017 - 12:52 PM
Posted 17 June 2017 - 04:52 PM
You have something like this:print("hello world")
What KingofGamesYami is suggesting is something like this:local function main() print("hello world") end local ok, err = pcall(main) if not ok then --# handle error print("error: "..err) --# print out the error end
Thats wha i have been doing but the problem is that the pat of the script is the GUI handling part and it gets an error if it is in a function so i moved it ou of the function and still want the error handling btw the method you guys told that was the one i was using also btw the error wasn't because of the error handling its that the GUI handling part was just timing out or something
Posted 17 June 2017 - 04:53 PM
Please post your code. It sounds like you have an unrelated issue which needs to be fixed.
Posted 17 June 2017 - 05:10 PM
there is no need for that i think, the Exact error is: dow97: vm error: java.lang.ArrayIndexOutOfBoundsExecption
Posted 17 June 2017 - 06:41 PM
That is a problem which needs to be fixed. That is the computer craft equivalent of a stack overflow. Most likely caused by recursive function calling.
Posted 18 June 2017 - 03:00 AM
That is to say: Don't try to use a function instead of a loop. By all means stick a proper loop (while/for/repeat) inside a function, but never have a function call itself over and over again.
Posted 18 June 2017 - 09:57 AM
Well if i use i won't be able to do the error handling so any solutions would be help full :)/>
Posted 18 June 2017 - 10:48 AM
We cannot suggest alterations to that which we cannot see.
Posted 18 June 2017 - 01:49 PM
`pcall` and `xpcall` are the only native ways of catching errors. They both accept functions as arguments. If you want to catch an error, you need the code to be inside a function to pass it to either. All Lua code can be placed inside a function and called without any differences:
If your code isn't working when put in a function, something else is going wrong, and you should address that issue. We can't help with an ambiguous error message and no source code, however. (i.e. give the source code and you'll get help!)
My best shot without source is that there's a chance you called your main function something that essentially overrides another function, so instead of calling that other function, it's calling itself, resulting in a stack overflow.
literally_any_code
--# is the same as
(function() literally_any_code end)()
with the slight exception of stack-related stuff being a little different, but inside CC, that's almost never a problem.If your code isn't working when put in a function, something else is going wrong, and you should address that issue. We can't help with an ambiguous error message and no source code, however. (i.e. give the source code and you'll get help!)
My best shot without source is that there's a chance you called your main function something that essentially overrides another function, so instead of calling that other function, it's calling itself, resulting in a stack overflow.
Posted 18 June 2017 - 02:33 PM
`pcall` and `xpcall` are the only native ways of catching errors. They both accept functions as arguments. If you want to catch an error, you need the code to be inside a function to pass it to either. All Lua code can be placed inside a function and called without any differences:with the slight exception of stack-related stuff being a little different, but inside CC, that's almost never a problem.literally_any_code --# is the same as (function() literally_any_code end)()
If your code isn't working when put in a function, something else is going wrong, and you should address that issue. We can't help with an ambiguous error message and no source code, however. (i.e. give the source code and you'll get help!)
My best shot without source is that there's a chance you called your main function something that essentially overrides another function, so instead of calling that other function, it's calling itself, resulting in a stack overflow.
We cannot suggest alterations to that which we cannot see.
here
Posted 18 June 2017 - 02:44 PM
Can you work out which of the last 3 lines causes the error? Comment the last two, then just the last one, then if it breaks, you'll know which line caused it.