lua-users home
lua-l archive

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

On Wed, Mar 9, 2011 at 1:02 PM, Axel Kittenberger <> wrote:
> In my opinion the core of that problem results in the Lua style of
> setting things to nil on error and hoping the nil will raise an error
> later on, instead of a more elaborated error system.

It's possible to wrap functions that return nil on error so that they
raise an error immediately:

local function throw_helper(...)
  if select(1,...) == nil then error("returned nil:
"..tostring(select(2,...)),3) end
  return ...

function throws(fun)
  return function(...)
    return throw_helper(fun(...))

Works like this:

function test(a)
    if a > 10 then return nil, "too big!" end
    return a+1

testx = throws(test)


and the last line will throw an error 'returned nil: too big!'

steve d.