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

try...catch (Error handling)

Started by KleptoKat, 05 April 2013 - 02:55 PM
KleptoKat #1
Posted 05 April 2013 - 04:55 PM
http://lua-users.org...1/msg00650.html

Although this is for c++ its basically self explanatory and I would love to see this in the next update.
Cranium #2
Posted 05 April 2013 - 05:21 PM
It's another suggestion that can be implemented client side with the tools already available to you.
theoriginalbit #3
Posted 05 April 2013 - 05:38 PM
-snip-
Where is the code? I cannot see it in that link.
KleptoKat #4
Posted 05 April 2013 - 05:51 PM
It's another suggestion that can be implemented client side with the tools already available to you.
Oh, really? I'll look into that further, then..
With a little more research, I've come up with this that I did not know was their previously: (Feel free to lock this topic.)


pcall (f, arg1, ···)
[indent=1]
Calls function f with the given arguments in protected mode. This means that any error inside f is not propagated; instead, pcall catches the error and returns a status code. Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, pcall also returns all results from the call, after this first result. In case of any error, pcall returns false plus the error message.[/indent]
Bubba #5
Posted 05 April 2013 - 09:13 PM
It's another suggestion that can be implemented client side with the tools already available to you.

Not to sound argumentative, but actually I don't believe it can be implemented with the tools available to us: not in that form anyway. If you take a look at the follow-up post, it's written in C as an extension for Lua.

But anyway, pcall does indeed do the same thing essentially.
theoriginalbit #6
Posted 05 April 2013 - 09:49 PM
pcall does indeed do the same thing essentially.
Just an annoying way about it. Since if you have multiple statements you want to pcall, you have to check for an error after each pcall as opposed to a try catch you define the catch once and then can try heaps of statements…
Cloudy #7
Posted 06 April 2013 - 12:20 AM
Oh sure. Let me just rewrite LuaJ's error handling because you don't know how to use pcall.

pcall does indeed do the same thing essentially.
Just an annoying way about it. Since if you have multiple statements you want to pcall, you have to check for an error after each pcall as opposed to a try catch you define the catch once and then can try heaps of statements…

You can capture lots of statements with pcall…
Xfel #8
Posted 06 April 2013 - 11:08 AM
If you have multiple statements to sourround with try/catch, put them in a closure. thanks to upvalues, you don't even have to declare argument.

And I don't think they would event try to change the lua language just for you. They won't even use lua 5.2, which is ready here.
Cloudy #9
Posted 06 April 2013 - 11:11 AM
Lua 5.2 is a compatibility breaking update.
Lyqyd #10
Posted 06 April 2013 - 11:25 AM
As in, "Lyqyd will no longer be compatible with the Ask a Pro section if everyone can use goto." ;)/>