lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Fri, Sep 17, 2010 at 6:16 PM, James Rhodes
<> wrote:
> I don't want Python, I want to modify Lua.  The standard try-catch
> patch is not flexible enough for my uses, as native functions in my
> engine will return different types of exceptions (and it's important
> to differentiate the type since not all thrown values will have a
> Message attribute that can be read).

i agree that try/catch syntax is easier to use than pcall(); but i've
rarely needed it.  this is my current approach to fake it for those
rare occasions:

---------- try.lua ----------------------
local function returner (c, r, ...)
	if r then return ... end
	return c(...)

return function (t, c)
	return returner (c, pcall(t))

usage sample:

---------- test_try.lua ----------
local try = require "try"

try (function ()
	print 'one'
	assert (true, 'not fail')
	print 'two'
	assert (false, 'do fail')
	print 'three'
end, function (e)
	print ('catch:', e)

#> lua test.lua
catch!   test.lua:7: do fail

definitely not worth modifying Lua just to add the keywords.

now, you bind the Lua core with your specific type system.... that's
just too far in the specialization.  Rob's suggestion is much more