lua-users home
lua-l archive

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




On 30/05/16 06:56 PM, Jonne Ransijn wrote:

function try(func)
    local catchers = {}
    local finalizers = {}
    return {
        catch = function(self, handle)
            catchers[#catchers+1] = handle
            return self
        end,
        finally = function(self, handle)
            finalizers[#finalizers+1] = handle
            return self
        end,
        run = function(self)
            local ok, err = pcall(func)
            if not ok then
                for n=1,#catchers do
                    ok, err = pcall(catchers[n], err)
                    if not ok then break end
                end
            end
            for n=1,#finalizers do
                local ok,2 err2 = pcall(finalizers[n])

"ok,2"?

                if ok then
                    ok = ok2
                    err = err2
                end
            end
            if not ok then error(err, 0) end
        end
    }
end

---------------

function genlog(s)
    print(s)
    error(s)
end

try(genlog "A")
:catch(genlog "B")
:catch(genlog "C")
:finally(genlog "D")
:finally(genlog "E")
:run()

Wouldn't genlog error as soon as you call it?

Prints:
A
B
D
E
D -- To stderr

But in the end it is just a design decision.

Wait why am I doing code reviews here again? .-. At least you tried, and proved your point: try(-catch)?(-finally)? isn't needed.

--
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.