Well when I mean "error("Error. You needed to make the function Piggy()")"
I auctally mean error()
As a means to exit the program. Terminate it in its footsteps.
But no error message.
Why do I not hear about this?
Why did I have to discover it on my own?
Is there something bad about it or something?
error() Why is it not well known?
Started by libraryaddict, May 08 2012 02:40 AM
10 replies to this topic
#1
Posted 08 May 2012 - 02:40 AM
#2
Posted 08 May 2012 - 05:26 PM
Well, I don't think that's the best way to close your program, since you are saying to the caller that an error ocurred. It's better to use return outside of functions, or break your program's loop.
Examples:
Examples:
local args = { ... }
if #args ~= 2 then -- check if you get the correct arguments
print("Usage: ...")
return -- exit the program
end
-- some more code
while true do -- infinite loop -- some code here if something then -- condition to exit the program break -- break the loop and exit end end
local bExit = false local function close() bExit = true end while not bExit do -- your program's code end
#3
Posted 09 May 2012 - 10:13 AM
idk.
When I do this its not very helpful
When I do this its not very helpful
print("First print!")
return
print("Shouldnt see this. But you will")
#4
Posted 09 May 2012 - 10:24 AM
libraryaddict, on 09 May 2012 - 10:13 AM, said:
idk.
When I do this its not very helpful
When I do this its not very helpful
print("First print!")
return
print("Shouldnt see this. But you will")
If you get the print afterwards, there is something you are doing wrong. Perhaps you are only exiting from a function?
#5
Posted 09 May 2012 - 01:30 PM
Im running that as a stand alone program
#6
Posted 09 May 2012 - 09:21 PM
Yeah you need to be in a function or loop for it to work. Try doing
Hmm nevermind that doesn't work either. I thought I had used something like this before to exit a program...
function returnTest()
print("First print!")
return
print("Shouldnt see this. But you will")
end
Hmm nevermind that doesn't work either. I thought I had used something like this before to exit a program...
Edited by luanub, 09 May 2012 - 09:24 PM.
#7
Posted 09 May 2012 - 09:26 PM
You're right - I'm sure this used to work from within the main body, but guess not.
The best way is to include all your code in a main() function - and then return from that when you want to end the program.
The best way is to include all your code in a main() function - and then return from that when you want to end the program.
#8
Posted 15 June 2012 - 07:43 AM
That does work, just call the function as well. so:
function returnTest()
print("First print!")
return
print("Shouldnt see this. But you will")
end
returnTest()
function returnTest()
print("First print!")
return
print("Shouldnt see this. But you will")
end
returnTest()
#9
Posted 15 June 2012 - 05:01 PM
luanub, on 09 May 2012 - 09:21 PM, said:
Yeah you need to be in a function or loop for it to work. Try doing
Hmm nevermind that doesn't work either. I thought I had used something like this before to exit a program...
function returnTest()
print("First print!")
return
print("Shouldnt see this. But you will")
end
Hmm nevermind that doesn't work either. I thought I had used something like this before to exit a program...
This is the problem with languages where whitespace is not significant(*cough*pythonisawesome*cough*). You have a newline there, but lua does not care. Lua just has specific rules it runs by. Any time you write "return" it takes everything up to the next "end" or the end of the file, and tries to return it. So, what you have there is equivalent to:
function returnTest()
print("First print!")
return print("The print will be run, and whatever it returns (nil) will be returned from the function")
end
To get the right behaviour, you have to put it in an artificial block, so it gets the end it expects.function returnTest()
print("First print!")
do return end
print("You wont see this, because you are now as smart as the machine :(/>/>")
end
#10
Posted 15 June 2012 - 05:53 PM
kazagistar, on 15 June 2012 - 05:01 PM, said:
luanub, on 09 May 2012 - 09:21 PM, said:
Yeah you need to be in a function or loop for it to work. Try doing
Hmm nevermind that doesn't work either. I thought I had used something like this before to exit a program...
function returnTest()
print("First print!")
return
print("Shouldnt see this. But you will")
end
Hmm nevermind that doesn't work either. I thought I had used something like this before to exit a program...
This is the problem with languages where whitespace is not significant(*cough*pythonisawesome*cough*). You have a newline there, but lua does not care. Lua just has specific rules it runs by. Any time you write "return" it takes everything up to the next "end" or the end of the file, and tries to return it. So, what you have there is equivalent to:
function returnTest()
print("First print!")
return print("The print will be run, and whatever it returns (nil) will be returned from the function")
end
To get the right behaviour, you have to put it in an artificial block, so it gets the end it expects.function returnTest()
print("First print!")
do return end
print("You wont see this, because you are now as smart as the machine :(/>/>")
end
In practice though, this doesn't cause an issue - you'd usually be returning from within a condition. Why would you put code after a return statement, if you expect the code to end at that point?
#11
Posted 15 June 2012 - 06:20 PM
I wasn't asking why, I was just trying to explain the example. The use of error for exiting seems like no problem to me, except in very strange cases, I guess.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











