error handling

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

error handling

fschmidt
Administrator
By default, Lua doesn't throw exceptions.  Instead it returns [nil, error].  Here Lua is going against how most languages work and I think Lua is wrong.  It is better to just throw exceptions and to have a try/catch statement if needed for error handling.  What do you think?
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: error handling

Hugo <Nabble>
I agree. I don't think people would have any issues with try/catch blocks.
Reply | Threaded
Open this post in threaded view
|

Re: error handling

fschmidt
Administrator
done in rev 43

There will be no pcall() or xpcall() functions.  Instead I will have:

"try" block "catch" Name "do" block "end"

The Name gets the exception.

The error() function accepts a LuaException as an argument in which case a new LuaException is thrown which shows the exception chaining in Lua.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: error handling

Hugo <Nabble>
Okay, sounds good.
Reply | Threaded
Open this post in threaded view
|

Re: error handling

fschmidt
Administrator
closing thread
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: error handling

fschmidt
Administrator
This post was updated on .
In reply to this post by fschmidt
Right now luan supports:
try
	-- something
catch e do
	print(e)
finally
	-- cleanup
end
This is an added concept, not in Lua.  And it doesn't seem to be used much.  Instead I could have a function Luan.try() used like this:
local Luan = require "luan:Luan"
local try = Luan.try

try {
	function()
		-- something
	end;
	catch = function(e)
		print(e)
	end;
	finally = function()
		-- cleanup
	end;
}
This is slightly uglier but is a standard function call passing a table.  So that eliminates one concept and three keywords.  What do you think?
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: error handling

Hugo <Nabble>
This reminds me of jquery. This is how we submit (POST) data to a given URL:
function fail() { ... };
function done() { ... };

var data = { text: '123' }
$.post(url, data).fail(fail).done(done);
What you suggested isn't much different than this. The problem of your approach is that the "try" block isn't a block, but a function call that takes a table. People must understand this concept first, otherwise they will be completely lost (I am not sure if they would be lost if they face the first example instead). But both solutions are fine to me as long as they are well documented (well, I slightly prefer the first piece of code though). People will have to read about this no matter what. Even though your suggestion has less concepts and keywords, chances are that people will recognize the first example from other languages they saw in the past. It is a trade off.
Reply | Threaded
Open this post in threaded view
|

Re: error handling

fschmidt
Administrator
Hugo <Nabble> wrote
People will have to read about this no matter what.
This is the part I disagree with.  Most users will never need try-catch, so they will never have to read about it.  (You didn't use try-catch at all in FinalJeans.)  But if it is part of the syntax of the langauge, then I have to document it in the basic language description.

Also, using a function, I will also add back the standard Lua functions pcall() and xpcall(), so this become much more standard.  I will go ahead.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: error handling

fschmidt
Administrator
In reply to this post by fschmidt
done in rev 327.  Now the reserved keywords in Luan exactly match Lua.  closing
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture