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

return error('This is an error message') or error('This too', 2)?

Started by Sewbacca, 14 July 2016 - 04:59 PM
Sewbacca #1
Posted 14 July 2016 - 06:59 PM
My question is:
What is better?
To return the error or to send it a plain above?
KingofGamesYami #2
Posted 14 July 2016 - 07:25 PM
They do the same thing, but the former has some restrictions on where you can use it. Also, in some cases you may want to blame the caller above the caller (3). This isn't enforced by the former
Lupus590 #3
Posted 14 July 2016 - 08:07 PM
error(message,2) I would use for functions/APIs reciving bad arguments. In my mind error(message) is for when your own code has an error
Sewbacca #4
Posted 14 July 2016 - 09:53 PM
They do the same thing, but the former has some restrictions on where you can use it. Also, in some cases you may want to blame the caller above the caller (3). This isn't enforced by the former

You can reach it, with return error(msg, 2).
Does handle pcall return error(msg) and error(msg, 2) the same?
Bomb Bloke #5
Posted 15 July 2016 - 12:40 AM
error(message,2) I would use for functions/APIs reciving bad arguments. In my mind error(message) is for when your own code has an error

Note it's "error(message,2)" vs "return error(message)". The latter performs a tail call, knocking the current function off the stack… meaning that the error will point to the calling function.

Thing is that you can stack multiple tail calls, so who knows where the error will actually end up pointing. I would consider manual specification of the error level to be more readable.