[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: __gc metamethod and error()
- From: "temp 213 qwe" <temp213@...>
- Date: Wed, 02 Jan 2019 16:27:18 +0600
On Tue, 01 Jan 2019 23:51:04 +0000
Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
<temp213@gorodok.net> writes:
temp> setmetatable ({}, {__gc = function() print(1) error("error") print(2) end})
temp> It looks like at the end, garbage collector also collects something required by error() function first. So there is no error message from the code above, lua 5.3.5.
"at the end"? If you mean when calling lua_close on the state, then the error() thrown is just ignored. __gc metamethods are always called via a pcall; in normal operation any error caught by that pcall is rethrown, interrupting the current round of finalization calls, but while closing the state it would be pointless to propagate the error (propagate it to what?) so it gets ignored instead.
I just wanted to use it as a hook to the "end of the script".
And find out that error() function does not work as usual.
I supposed it should be equal to automatic starting garbage
collector as last step.
t = {}
setmetatable (t, {__gc = function() print(1) error("error")
print(2) end})
--
t = nil
collectgarbage()