lua-users home
lua-l archive

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


What happens when closing methods error?

To make the test short and simple, I added a closing method to
strings, with a silly error condition (closing errors when string
length is 3 -- see code below)

    --let's define a closing method on strings
    -- (just to make the test simpler/shorter)
    string_mt = getmetatable('a')
    function string_mt.__close(s)
        print("closing " .. s)
        if #s == 3 then
            print("before error " .. s)
            error("err closing ".. s) end
    end

    function test1()
    local <toclose> x='XXX'-- closing should error
    end

    function test2()
    --~ local <toclose> x='XXX'-- closing should error
    local <toclose> y='YY' -- closing is ok
    local <toclose> z='ZZZ' -- closing should error
    print("x, z are in scope: ", x, z, "let's raise an error")
    assert(false, "something is wrong!")
    print("Is this executed? No, the assert above has raised an error")
    end

    -- test1()

    test2() -- the _last raised_ error is reported. no traceback

with test1(), the program runs as expected. "Closing" 'x' errors. The
closing error is reported with a traceback.

with test2(), the _last_ closing error is reported (i.e. closing 'x').
There is no traceback. The first encoutered errors (the failed assert
withing the function body, and the error closing 'z') are raised, but
then not reported..

With this behavior, it looks like an error within the body of a block
is "masked" by any error in a closing method.

I think the expected behavior would be:

1. attempt to execute all the closing methods (as Lua-5.4.0 does now),
but report _the first encountered error_, instead of the last (in this
example, reporting the assert failure)

2. include by default a traceback for the reported error even if there
are more than one closing errors

Phil