lua-users home
lua-l archive

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




On 30/05/16 05:14 PM, Jonne Ransijn wrote:

function try(func)
    local ok, err = pcall(func)
    return {
        catch = function(self, handle)
            if not ok then
                handle(err)
            end
            return self
        end,
        finally = function(self, handle)
            handle()
            return self -- Optional
        end
    }
end

try(function()
    error "Error!"
end)
:catch(function(err)
    print(err)
end)
:finally(function()
    print "Finally!"
end)

-- Same as:

local ok, err = pcall(function() error "Error!" end)
if not ok then
    print(err)
end
print "Finally!"

I think the chances of Lua having try-catch-finally statements would be zero, pcall is good enough.

What about error() in catch()?

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.