lua-users home
lua-l archive

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


phlnc8 wrote:
> 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)

In C# the last error is reported and I actually think that is the right
decision.
<https://stackoverflow.com/questions/2911215/what-happens-if-a-finally-block-throws-an-exception>

Consider this program with two errors, where at least one is a
programming mistake:

  do
    local z
    local <toclose> resource = setmetatable({}, {
      __close = function ()
        z.closed = true -- error 2, z might be nil
      end
    })

    error "something went wrong" -- error 1
    z = {}
  end

If the first error is reported I will never even know that there was a
second error. And once I fixed the first error, I probably won't see
that there is problem with my __close function.

However, if the last error is reported, I can fix that and after fixing
it I will still see the first error.

Best regards,

David